如何解决Python-binance 包装器 ConnectionClosedError: 代码 = 1006
我编写了一个程序,使用 python-binance 从 binance websocket 获取最新的蜡烛。 一切正常,除了在长时间的测试后给我这个错误(我在昨天晚上的测试中得到了 1 次,今天在大约 8 小时内得到了 4 次:
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
我有一个循环来检查套接字是否正在接收数据,如果在 x 秒内没有新数据,则重新连接。 奇怪的是,出错后一切正常,没有重新连接,看起来我得到了数据。
我尝试使用调用 python-binance 包装器的 try 来捕获错误,以确保在它发生时我可以重新连接,但它不起作用。
我调用包装器的脚本:
try:
twm = ThreadedWebsocketManager()
twm.start()
ctime = current_time_millis() + 5
twm.start_kline_socket(process_kline_data,market,interval[0])
print(interval[0],"kline has been started")
except Exception as e:
print(e,"THIS IS THE Kline ticket",interval)
else:
kline_streams.append([market,interval[0],twm,ctime])
kline_to_send[market] = {}
我意识到我无法捕获错误,因为它是线程化的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。