sammchardy / python-binance

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

ThreadedWebsocketManager() #1409

Open pdb159 opened 3 months ago

pdb159 commented 3 months ago

Describe the bug I'm using python-binance's example of a threaded websocket manager, to retrieve BTCUSDT prices. When running the script within jupyter lab i receive the follwoing RuntimeError:Exception in thread Thread-9: Traceback (most recent call last): File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\site-packages\binance\threaded_stream.py", line 59, in run self._loop.run_until_complete(self.socket_listener()) File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\lib\asyncio\base_events.py", line 625, in run_until_complete self._check_running() File "C:\Users\pb\Documents\Python_Anaconda3\envs\CryptoPredictor\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

If running it in PyCharm i don't receive any errors but also no output and nothing seems to be happening.

To Reproduce `import time

from binance import ThreadedWebsocketManager

api_key = '<api_key'> api_secret = '<api_secret'>

def main():

symbol = 'BNBBTC'

twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
# start is required to initialise its internal loop
twm.start()

def handle_socket_message(msg):
    print(f"message type: {msg['e']}")
    print(msg)

twm.start_kline_socket(callback=handle_socket_message, symbol=symbol)

# multiple sockets can be started
twm.start_depth_socket(callback=handle_socket_message, symbol=symbol)

# or a multiplex socket can be started like this
# see Binance docs for stream names
streams = ['bnbbtc@miniTicker', 'bnbbtc@bookTicker']
twm.start_multiplex_socket(callback=handle_socket_message, streams=streams)

twm.join()

if name == "main": main()` Expected behavior A clear and concise description of what you expected to happen.

Environment (please complete the following information):

Logs or Additional context Jupyter Lab Python Setup:

PyCharm Setup:

Any Ideas on how to fix this? I saw different posts about this from 2022 and 2023 and was wondering if it is still the same fix, replacing some line of code within the python-binance library