'Python-binance wrapper ConnectionClosedError: code = 1006
I wrote a program to get the latest candles from binance websocket using python-binance. Everything works fine, except it gives me this error after long testing hours (I got it 1 time during yesterdays night test and 4 time today in approx 8 hours:
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending name='Task-3596198' coro=<ReconnectingWebsocket._read_loop() running at /home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/binance/streams.py:128> wait_for=<Future finished result=None>>
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-3606677' coro=<WebSocketCommonProtocol.recv() done, defined at /home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py:360> exception=ConnectionClosedError('code = 1006 (connection closed abnormally [internal]), no reason')>
Traceback (most recent call last):
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 750, in transfer_data
message = await self.read_message()
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 819, in read_message
frame = await self.read_data_frame(max_size=self.max_size)
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 895, in read_data_frame
frame = await self.read_frame(max_size)
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 971, in read_frame
frame = await Frame.read(
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/framing.py", line 55, in read
data = await reader(2)
File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
await self._wait_for_data('readexactly')
File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data
await self._waiter
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 421, in recv
await self.ensure_open()
File "/home/renato/PycharmProjects/Trading_bot/venv/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 735, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
I have a loop to check if the sockets are receiving data as they should and reconnect if there is no new data in x seconds. The strange thing that everything works just fine after the error, no reconnect and it looks like I get the data.
I tried to catch the error using try where I call the python-binance wrapper to make sure I could reconnect when it happens, but it does not work.
The script where I call the wrapper:
try:
twm = ThreadedWebsocketManager()
twm.start()
ctime = current_time_millis() + 5
twm.start_kline_socket(process_kline_data, market, interval[0])
print(interval[0], market,"kline has been started")
except Exception as e:
print(e, "THIS IS THE Kline ticket", market, interval)
else:
kline_streams.append([market, interval[0], twm, ctime])
kline_to_send[market] = {}
I realized that I can not catch the error because it is threaded.
Is there a way to catch the error without going deep in modifying the wrapper?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
