sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.89k stars 2.2k forks source link

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

Open bairavand opened 1 year ago

bairavand commented 1 year ago

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:

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

thiemo-seys commented 1 year ago

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

bairavand commented 1 year ago

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.

OpenCoderX commented 9 months ago

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

OpenCoderX commented 8 months ago

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

vzhilov commented 8 months ago

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