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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...