python-binance: Queue overflow error, change MAX_QUEUE_SIZE value
My code:
# callback
def process_message_price(msg):
print(msg)
# websocket
bm = ThreadedWebsocketManager()
bm.start()
# listOfPairings: all pairs with USDT (over 250 items in list)
for pairing in listOfPairings:
conn_key = bm.start_trade_socket(callback=process_message_price,symbol=pairing)
bm.join()
hovewer after shor time, i am getting following error: ‘e’: ‘error’, ‘m’: ‘Queue overflow. Message not filled’
which is caused by MAX_QUEUE_SIZE in streams.py being too small for my program
How can i change this value outside of streams.py file ?
Thx
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 21
I was just playing around with the documentation code and got this same error when I was manually using the Asynchronous context manager.
What sorted it out for me was once you open the connection, you recieve the message you should close the context manager Here is my code:
Its the line
await socket.__aexit__(None, None, None)
that sorted out the issue for meAnd put MAX_QUEUE_SIZE = 10000 There is no other way to fix this error I have no more than 10-15 restarts of the web socket on all USDT pairs per day (while the restart occurs within 5-7 seconds after the error)
Calling the restart of the stream from except did not work for me either, only in the function as a separate thread
Hello,
FYI, I have test this solution and it is not really working, just the part below have no “else” statement in the v1.0.13:
but the queue is still overflow and we still lose some messages,
My solution is: I increase the limit to 10000 and I have change in the threaded_stream.py the wait_for from 3 to 7:
I have test for 4 hours straight and I had the “else” statement to log when the queue is overflow, and it never happen again. everything looks good for me
Best regards
You have to download the python-binance package from source and use it in your code https://github.com/sammchardy/python-binance/tree/master/binance
then replace the streams.py with an outdated version (v1.0.13) of the file from source https://github.com/sammchardy/python-binance/tree/v1.0.13/binance
I think there is still some issue/bug in
ThreadedWebsocketManager
but I did not have the time yet to investigate. I also assumed first that the issue is my callback being too slow, so I’ve implemented a reconnect/reset whenever the error appears, however it only appears once at the very beginning, and afterwards it runs fine without reconnect/reset.Maybe I’m wrong, so if somebody has the knowledge and time please have a look at my solution:
This issue is a feature rather than a bug(Preventing from memory leak). It only means the msg handler can not consume the msg as fast as the receiver. Memory usage will increase unlimitiedly without the MAX_QUEUE_SIZE. The solution other comments has proposed went the wrong way.
And the solution is obvious, just imporve the efficiency of the msg handler, or split pairs into multiple processes.
Saved my day!! Thanks.
But why are we getting this error? Is it because of the asynchronous function we used?
I tried to directly use the websocket, seems good for me.
ws = websocket.WebSocketApp(f"wss://fstream.binance.com/ws/!bookTicker", on_message=on_message, on_error=on_error)
ws.run_forever()
Even if you make MAX_QUEUE_SIZE equal to 10000 (or more, it doesn’t matter), in the end you will still get this error or the stream stops working