LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
684 stars 165 forks source link

High cpu consumption of a thread #162

Closed bashman closed 3 years ago

bashman commented 3 years ago

Hi. In the last few days I have been experiencing high CPU consumption of one of the threads (see picture). The situation is that, after about 40 minutes, it begins to happen. WS is quiet, there is no activity. I am working with user data stream. Can you identify the thread that is causing problems?

LInux 64 Python 3.8 U.B.W.A git master AMD (8 cores)

imagen

oliver-zehentleitner commented 3 years ago

Can you provide code to reconstruct the error? Could also be the thread that is processing the data....

bashman commented 3 years ago

Can you provide code to reconstruct the error? Could also be the thread that is processing the data....


while True:
if binance_websocket_api_manager.is_manager_stopping(): 
exit(0)
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
  if oldest_stream_data_from_stream_buffer is False:
     time.sleep(0.05)

  else:
     try:
        json_reply = json.loads(oldest_stream_data_from_stream_buffer)
        now = datetime.now()
        now = now.timestamp()
        event_type = json_reply['e']  # Event type

        if (event_type == 'ACCOUNT_UPDATE'):
           if (json_reply['a']['P'][0]['s'] == 'ADAUSDT'):
              account_update = json_reply            
              print(account_update)                  
              #{'e': 'ACCOUNT_UPDATE', 'T': 1617571446767, 'E': 1617571446772, 'a': {'B': [{'a': 'USDT', 'wb': '100.48569392', 'cw': '76.05644052'}], 'P': [], 'm': 'ADMIN_DEPOSIT'}}  
              #when P is empty
              try:
                 balance = float(account_update['a']['P'][0]['pa'])    # capital   
              except Exception as e:
                 pass
              finally:
                 pass

        if ((event_type == 'ORDER_TRADE_UPDATE') and (json_reply['o']['s'] == 'ADAUSDT')):
            print(json_reply)

           if (current_order_status == 'CANCELED'):
              pass

     except Exception:
        # not able to process the data? write it back to the stream_buffer
        binance_websocket_api_manager.add_to_stream_buffer(oldest_stream_data_from_stream_buffer) 

You can start the script without problems, but at a certain point, after a few minutes the issue occurs. 
oliver-zehentleitner commented 3 years ago

what json lib do you use, what happens if you dont convert it to a dict and just let it run?

oliver-zehentleitner commented 3 years ago

what would really help is, if you make a script as short as possible, run it and if the error still happens upload the full file here.

bashman commented 3 years ago

what would really help is, if you make a script as short as possible, run it and if the error still happens upload the full file here.

Yes.

idkravitz commented 3 years ago

It may be pop/add infinite loop, because Exception isnt handled in any way, instead message is put back in the buffer to try next time. If this is so it has nothing to do with ubwa, but with the user code provided here. Try put some debug print after the comment # not able to process the data? write it back to the stream_buffer to see if that's the case

oliver-zehentleitner commented 3 years ago

@bashman how do we continue? do you provide a full script or do we close this?