python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Lagging issue on other threads when using start_depth_socket

Open pazooki opened this issue 3 years ago • 0 comments

Describe the bug When starting a new thread; twm.start_depth_socket it blocks long enough that I get lagging issue on twm.start_aggtrade_socket and the price data becomes out of date. I've timed strategy.update_depth and reduced it to just setting an instance variable to store the data coming into twm.start_depth_socket but the problem still persists. I have also tried to use ThreadedDepthCacheManager and that had the same problem of causing a lag on other threads.

To Reproduce Code snippet to reproduce the behavior:

BTCUSDT = 'BTCUSDT'

def main(strategy, is_testing):
    config = TESTNET_CONFIG if is_testing else CONFIG

    twm = ThreadedWebsocketManager(api_key=config['API_KEY'], api_secret=config['API_SECRET'], testnet=is_testing)
    # dcm = ThreadedDepthCacheManager(api_key=config['API_KEY'], api_secret=config['API_SECRET'], testnet=is_testing)
    # dcm.start()
    twm.start()
    twm.start_depth_socket(callback=strategy.update_depth, symbol=BTCUSDT, depth=10, interval=1)
    twm.start_aggtrade_socket(strategy.next, symbol=BTCUSDT)
    twm.start_user_socket(strategy.update_balance)
    # dcm.join()
    twm.join()

Expected behavior No lagging on other threads when I use twm.start_*

Environment (please complete the following information):

  • Python version: 3.11.3
  • Virtual Env: virtualenv
  • OS: Fedora 38
  • python-binance version: 1.0.17

Logs or Additional context

I am sharing the strategy object among twm.start_depth_socket and twm.start_aggtrade_socket and twm.start_user_socket however there is no conflict or race condition since they each update their own variables without updating variables belonging to other threads. The problem I'm seeing is that the price that I receive in start_aggtrade_socket is lagged behind and outdated and when I watch and compare it to the Binance Client price. I tried it only on TESTNET

pazooki avatar Apr 28 '23 17:04 pazooki