Skip to content

Cannot create ThreadedWebsocketManager and Websocket API in the same thread #1575

@monkingxue

Description

@monkingxue

Describe the bug
I want to create a ThreadedWebsocketManager to subscribe to infomation updates and create a Client to place an order(client.ws_futures_create_order) via websocket api, but when I start the program reports an error This event loop is already running.

To Reproduce

from binance import ThreadedWebsocketManager, Client

api_key = ''
api_secret = ''

def main():

    symbol = 'BNBBTC'

    client = Client(api_key=api_key, api_secret=api_secret)
    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
    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)

    account = client.ws_futures_v2_account_status()
    print(f"account: {account}")

    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):

  • Python version: 3.9
  • Virtual Env: pipenv
  • OS: Mac
  • python-binance 1.0.28

Logs
Please set logging to debug and paste any logs here, or upload debug.log file to the issue.

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    main()
  File "test.py", line 21, in main
    account = client.ws_futures_v2_account_status()
  File "/Users/.local/share/virtualenvs/datainfra-gsnu_lej/lib/python3.8/site-packages/binance/client.py", line 13286, in ws_futures_v2_account_status
    return self._ws_futures_api_request_sync("v2/account.status", True, params)
  File "/Users/.local/share/virtualenvs/datainfra-gsnu_lej/lib/python3.8/site-packages/binance/base_client.py", line 389, in _ws_futures_api_request_sync
    return self.loop.run_until_complete(
  File "/Users/.pyenv/versions/3.8.16/lib/python3.8/asyncio/base_events.py", line 592, in run_until_complete
    self._check_running()
  File "/Users/.pyenv/versions/3.8.16/lib/python3.8/asyncio/base_events.py", line 552, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions