binance-connector-python
binance-connector-python copied to clipboard
ReactorNotRestartable
Issue subject
When I run the example websocket code twice, I get an error.
Code to run
from binance.websocket.spot.websocket_client import SpotWebsocketClient as WebsocketClient
def message_handler(message):
print(message)
ws_client = WebsocketClient()
ws_client.start()
# Combine selected streams
ws_client.instant_subscribe(
stream=['bnbusdt@bookTicker', 'ethusdt@bookTicker'],
callback=message_handler,
)
ws_client.stop()
ws_client = WebsocketClient()
ws_client.start()
Error Message
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\threading.py", line 954, in _bootstrap_inner
>>> self.run()
File "C:\Program Files\Python39\lib\site-packages\binance\websocket\binance_socket_manager.py", line 74, in run
reactor.run(installSignalHandlers=False)
File "C:\Program Files\Python39\lib\site-packages\twisted\internet\base.py", line 1422, in run
self.startRunning(installSignalHandlers=installSignalHandlers)
File "C:\Program Files\Python39\lib\site-packages\twisted\internet\base.py", line 1404, in startRunning
ReactorBase.startRunning(cast(ReactorBase, self))
File "C:\Program Files\Python39\lib\site-packages\twisted\internet\base.py", line 843, in startRunning
raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable
Environment
Provide any relevant information about your setup, such as:
- Version of binance-connector-python: binance-connector==1.7.0
- Python version: Python 3.9.2
- Operating system: Windows 10
Seems like we cannot restart the websocket client. I gave time.sleep(100)
between stopping the connection and making one again but still did not work.
Only way I can restart is by running the python code again from the start.
When the websocket connection is closed, the reactor
( from twisted.internet
) needs to be closed as well and once this happens it can’t be restarted again within the same process (reference), so there’s not much we can do here, however we’ll explore adding an unsubscribe method to stop receiving events from streams and then able to subscribe again without closing the webscoket connection itself.
So where is the unsubscribe method?
@veckybin There's an internal ticket for it but without ETA for now.