How can I solve my problem with Binance Python package?

74 Views Asked by At

I want to run following code in Google colab but my code falls in a loop and back an error:

RuntimeError: This event loop is already running

import time

from binance import ThreadedWebsocketManager

api_key = ''
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)
    twm.start_depth_socket(callback=handle_socket_message, symbol=symbol)

    twm.join()


if __name__ == "__main__":
   main()

and error:

Exception in thread Thread-11:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/binance/threaded_stream.py", line 59, in run
    self._loop.run_until_complete(self.socket_listener())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 625, in run_until_complete
    self._check_running()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 584, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
0

There are 0 best solutions below