Skip to content
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

ERROR: CANCEL read_loop, websocket updates loss #1465

Closed
Ramesses3 opened this issue Nov 9, 2024 · 5 comments
Closed

ERROR: CANCEL read_loop, websocket updates loss #1465

Ramesses3 opened this issue Nov 9, 2024 · 5 comments
Assignees

Comments

@Ramesses3
Copy link

Bug description
I'm trying to get kline data via a web socket. Interested in closing candles. I noticed that some of the data is lost, although if you check in parallel through postman, then all updates come there.
Also, my python script periodically throws a CANCEL read_loop error. I think the data loss is related to it.

To Reproduce
Run my simple code:

#!/usr/bin/env python3
import re
import time
import asyncio
from datetime import datetime
from binance import AsyncClient, BinanceSocketManager

async def websocket(client=None, symbol=None):
  bm = BinanceSocketManager(client)
  symbol = symbol.lower()
  ms = bm.multiplex_socket([
    f'{symbol}@kline_1m',
    f'{symbol}@kline_5m',
    f'{symbol}@kline_15m'
  ])
  while True:
    async with ms as stream:
      msg = await stream.recv()
      if msg.get('data').get('k').get('x'):
        interval = re.search(r'kline_(.+)', msg.get('stream')).group(1)
        otime = datetime.fromtimestamp(msg.get('data').get('k').get('t')/1000)
        print(f'{interval} {otime}\n{msg}\n')

async def main():
  client = await AsyncClient.create(testnet=False)
  await asyncio.gather(
    websocket(client=client, symbol='TRBUSDT'),
  )

asyncio.run(main())

Expected behavior
Continuous acquisition of all data from the stream(s) without loss.

Environment:

  • Python version: 3.11.9
  • Virtual Env: no
  • OS: CentOS
  • python-binance version 1.0.22
@pcriadoperez pcriadoperez self-assigned this Nov 9, 2024
@Ramesses3
Copy link
Author

I just wrote my simple multiplex socket using pure websockets version 13.1. It works just great, no errors, no data loss.
It turns out that the problem is in python-binance.

@carlosmiei
Copy link
Collaborator

@Ramesses3, can you assemble a simple script that uses your implementation and python-binance simultaneously, compares the output, and throws an error when one starts to lag behind?

@pcriadoperez
Copy link
Collaborator

pcriadoperez commented Nov 30, 2024

Hi @Ramesses3 ,

I was able to reproduce the error. The issue is that the while loop needs to be inside the stream context:

    async with ms as stream:
        while true:

If not the stream will continuously connect and reconnect after each message, and this is whats causing the Cancel read loop log and the probable loss of data

I will adjust the logs and docs so this is easier to find the in the future. I also struggled a while from the docs, as it is showing up as an error when it shouldn't.

@Ramesses3
Copy link
Author

Hi @pcriadoperez ,
Thank you! I'll give it a try.

@Ramesses3
Copy link
Author

Ramesses3 commented Dec 1, 2024

@pcriadoperez thank you one more time. Now it works fine!

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

3 participants