Python-binance: how do i exit BinanceSocketManager?

Created on 21 Dec 2017  路  4Comments  路  Source: sammchardy/python-binance

bm = BinanceSocketManager(client)
conn_key = bm.start_depth_socket(symbol, process_book, depth=BinanceSocketManager.WEBSOCKET_DEPTH_5)
bm.start()
bm.close()

after bm.close(), still receive message

Most helpful comment

Ok I think I narrowed the bug down to the Twisted Library. BinanceSocketManger calls reactor.start(), but it never stops it. So the reactor keeps running even after I call BinanceSocketManager.close(). I think one solution would be have a destructor that counts the instances of BinanceSocketManager or whatever classes use the twisted reactor and once the count hits 0, it calls reactor.stop(). Until then, put this bit of code at the end of your program and it stops fine:

from twisted.internet import reactor
reactor.stop()

All 4 comments

@marzwu can you check you have the latest version v0.5.6. There were some changes recently that may help that issue.

What's also happening in your example is that calling close directly after start hasn't allowed the socket to initialise yet so it has nothing to close.

If you try

import time
bm = BinanceSocketManager(client)
conn_key = bm.start_depth_socket(symbol, process_book, depth=BinanceSocketManager.WEBSOCKET_DEPTH_5)
bm.start()
time.sleep(5) # simulate doing something and allow time to connect
bm.close()

You should see that it does close as expected.

I have the same problem as @marzwu, I have version 0.5.6, and I used the code you provided above. However, the program hangs on bm.close(). It just sits there until I manually stop the program. When I throw a KeyboardInterrupt, Python gives back this stack trace:

Exception ignored in: <module 'threading' from '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py'>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 1295, in _shutdown
    t.join()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 1061, in join
    self._wait_for_tstate_lock()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 1077, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

Somewhere it looks like the code is waiting for some thread to end but it never does. When I run the same code except that I exclude the time.sleep(5) line, it's as if the bm.close() line never get called, the code keeps receiving and processing messages from Binance. I'll look more into this, but I think there's a bug with BinanceSocketManager.close

Ok I think I narrowed the bug down to the Twisted Library. BinanceSocketManger calls reactor.start(), but it never stops it. So the reactor keeps running even after I call BinanceSocketManager.close(). I think one solution would be have a destructor that counts the instances of BinanceSocketManager or whatever classes use the twisted reactor and once the count hits 0, it calls reactor.stop(). Until then, put this bit of code at the end of your program and it stops fine:

from twisted.internet import reactor
reactor.stop()

@jeffro256 yes you're correct. You can use the reactor.stop() method in your code if you want to exit.
Twisted doesn't allow running multiple reactor loops from what I can see. So it's difficult to know when to call stop. The user may be running a depth cache, but want to stop the separate websocket connections separately. I might just put a note in about reactor.stop() as a first step.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fj317 picture fj317  路  6Comments

terenceljz picture terenceljz  路  3Comments

noahwoodin picture noahwoodin  路  8Comments

bilgecetin picture bilgecetin  路  6Comments

scalpounet picture scalpounet  路  6Comments