binance-connector-python icon indicating copy to clipboard operation
binance-connector-python copied to clipboard

ReactorNotRestartable

Open k128 opened this issue 3 years ago • 5 comments

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

k128 avatar Nov 12 '21 22:11 k128

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.

veckybin avatar Nov 17 '21 07:11 veckybin

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.

aisling11 avatar Dec 03 '21 15:12 aisling11

So where is the unsubscribe method?

veckybin avatar Dec 10 '21 04:12 veckybin

@veckybin There's an internal ticket for it but without ETA for now.

aisling11 avatar Dec 10 '21 11:12 aisling11