Skip to content

ThreadedWebsocketManager instance calls start_kline_futures_socket() from another thread and does not print kline data #1543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tonytao0 opened this issue Jan 9, 2025 · 2 comments
Assignees

Comments

@tonytao0
Copy link

tonytao0 commented Jan 9, 2025

test.py:

from binance import ThreadedWebsocketManager

import time
import threading

twm: ThreadedWebsocketManager
symbol = "BTCUSDT"

def handle_socket_message(msg):
    print('handle_socket_message',msg)

def test_threaded_socket_manager():
    global twm
    print('test_threaded_socket_manager run')
    print('id:',id(twm))
    print('type:',type(twm))

    sock_id = twm.start_kline_futures_socket(callback=handle_socket_message, symbol=symbol)
    time.sleep(10)
    twm.stop_socket(sock_id)

    print('test_threaded_socket_manager stop')

if __name__ == '__main__':
    twm = ThreadedWebsocketManager(api_key="", api_secret="", testnet=False)
    twm.start()
    print('id:',id(twm))
    print('type:',type(twm))

    t = threading.Thread(target=test_threaded_socket_manager)
    t.start()
    t.join()
 
    twm.stop()
    print('quit')

run and print:

  id: 139899228454864
  type: <class 'binance.ws.streams.ThreadedWebsocketManager'>
  test_threaded_socket_manager run
  id: 139899228454864
  type: <class 'binance.ws.streams.ThreadedWebsocketManager'>
  test_threaded_socket_manager stop
  quit
@tonytao0
Copy link
Author

tonytao0 commented Jan 9, 2025

ThreadedWebsocketManager instantiated and called start_kline_futures_socket on the same thread worked fine

However, different threads cannot receive kline data when one thread is instantiated while another thread calls start_kline_futures_socket function

@pcriadoperez
Copy link
Collaborator

Hi @tonytao0 ,

Thanks for reporting.
So each thread should create its own TWM instance, TWM instance should be created and used in the same thread and not shared across threads as each TWM manages its own async event loop. The processing and callback you can put in a different thread.

Can I ask, why are you trying to separate into different threads?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants