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

Thredpool Problem

Open Savadiv opened this issue 8 years ago • 3 comments

Greetings,

I am trying to observe every single coin on Binance. These are approx. 150 coins. To do this I want to use websockets, since its much more faster than single Client queries. To be concrete: I want to have three websockets for every coin: 1h klines, 1m klines and ticker. My problem is sadly, that when I have many coins I get an error from twisted as follows:

Unhandled Error
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  File "/Library/Python/2.7/site-packages/binance/websockets.py", line 436, in run
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1195, in run
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1204, in mainLoop
--- <exception caught here> ---
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 825, in runUntilCurrent
  File "/Library/Python/2.7/site-packages/twisted/internet/tcp.py", line 414, in resolveAddress
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 570, in resolve
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 271, in getHostByName
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 992, in getThreadPool
  File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 957, in _initThreadPool
    
exceptions.ImportError: cannot import name threadpool

Is there any workaround or solution to this?

Thank you very much!

Savadiv avatar May 01 '18 11:05 Savadiv

I'm not sure about that error message, but I'm sure there's no reason to use a separate socket for each stream because you can just use a single multiplex socket for all of them (maybe except the user stream, though I'm not even sure about that anymore). I'm listening to 305 aggregate trade streams plus the mini ticker this way, and it's working perfectly. For your app, and throwing in the mini ticker just for kicks:

def callback(stuff):
    stream, data = stuff['stream'], stuff['data']
    print(stream, data)

streams = ['!miniTicker@arr@3000ms'] + [symbol + '@kline_1h' for symbol in symbols] + [symbol + '@kline_1m' for symbol in symbols]
bm.start_multiplex_socket(streams, callback)

wranai avatar May 01 '18 18:05 wranai

@wranai can you plz help me close one symbol stream on the multiplex socket i tried the bm.close_socket(conn_key) and the bm.close() didnt work

dikano avatar Sep 02 '18 15:09 dikano

@dikano I just saw your separate issue about that. As I wrote it there, it isn't possible. The Binance websocket API is not a pub/sub thing like e.g. the Poloniex one. You open the socket referencing everything you want to open and that's what the socket is about until you close it. Honestly, there's no reason to remove stuff from a socket; you can just ignore what you don't care about at the moment.

wranai avatar Sep 02 '18 18:09 wranai