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

python-binance Websocket imcompatability with Python 3.10. Also not working with Python version greater than Python 3.7.

Open bairavand opened this issue 2 years ago • 5 comments

Describe the bug I was using python-binance with Python 3.7 and it was working fine. I upgraded Python version to 3.10 and I unable to get websocket response. I also tried with Python 3.8 that is also not working. Is there any way to handle it?

To Reproduce Code snippet to reproduce the behavior:

client = await AsyncClient.create(api_key='', api_secret='', testnet=True)
bm = BinanceSocketManager(client, user_timeout=3600)
# start any sockets here, i.e a trade socket
ts = bm.futures_user_socket()
# then start receiving messages
async with ts as tscm:
    while True:
        res = await tscm.recv()

Expected behavior Inside the while loop the trade response should come whenever a trade happens on Binance. but after upgraded to Python 3.10 the response is not triggered and I didn't receive any error. Environment:

  • Python version: 3.10
  • Virtual Env: virtualenv
  • OS: Mac and Ubuntu
  • python-binance version 1.0.17

Logs or Additional context I didn't receive the websocket response. Other than that, there is no specific error

bairavand avatar Jun 23 '23 04:06 bairavand

Did you manage to find a solution yet? Im running into the same issue

thiemo-seys avatar Jun 26 '23 19:06 thiemo-seys

I didn't find any solution yet. I planned to switch to an another library called binance-connector which is currently supporting Python 3.10 and also it's an official Binance library.

bairavand avatar Jun 27 '23 10:06 bairavand

Another user opened issue https://github.com/sammchardy/python-binance/issues/1354 that seems related.

jbsmithj1981 avatar Oct 11 '23 02:10 jbsmithj1981

@bairavand Do you also have an instance of ThreadedDepthCacheManager in your application?

jbsmithj1981 avatar Oct 29 '23 05:10 jbsmithj1981

With the simple script like below I get the same error, but not the ThreadedDepthCacheManager one. Also no scripts from the examples work for me giving the same error.

import time
from binance import ThreadedWebsocketManager

def on_message(message):
    event_time = message['data']['E']
    close_time = message['data']['k']['T']
    close_price = message['data']['k']['c']
    print(f"Event Time: {event_time}, Close Time: {close_time}, Close Price: {close_price}")

symbol = 'BNBBTC'
interval = '1m'

twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
twm.start()

twm.start_kline_socket(callback=on_message, symbol=symbol)
twm.join()

while True:
    time.sleep(1)

Exception in thread Thread-5: Traceback (most recent call last): File "C:\ProgramData\miniconda3\envs\streams\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\ProgramData\miniconda3\envs\streams\lib\site-packages\binance\threaded_stream.py", line 56, in run self._loop.run_until_complete(self.socket_listener()) File "C:\ProgramData\miniconda3\envs\streams\lib\asyncio\base_events.py", line 625, in run_until_complete self._check_running() File "C:\ProgramData\miniconda3\envs\streams\lib\asyncio\base_events.py", line 584, in _check_running raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running C:\ProgramData\miniconda3\envs\streams\lib\threading.py:1018: RuntimeWarning: coroutine 'ThreadedApiManager.socket_listener' was never awaited self._invoke_excepthook(self) RuntimeWarning: Enable tracemalloc to get the object allocation traceback

vzhilov avatar Oct 30 '23 08:10 vzhilov