sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.06k stars 2.21k forks source link

unable to reconnect when using feature/asyncio branch of websocket.py #526

Open CyrilleBoullier opened 4 years ago

CyrilleBoullier commented 4 years ago

Hello,

I'm using the branch feature/asyncio and it works well.

However, due to binance deconneciton of the websocket, I sometime recieved following messages:

and it seems that the API does not reconnect correctly.

I tried to reproduce it using following snippet:

import asyncio

from binance import AsyncClient, BinanceSocketManager

import logging from logs_management import logToConsole logToConsole(logging.INFO)

loop = None

async def main(): global loop

# initialise the client
client = await AsyncClient.create()

# initialise socket manager
bsm = BinanceSocketManager(client, loop)

# setup async callback handler for socket messages
async def handle_evt(msg):
    pair = msg['s']
    print(f'{pair} : {msg}')

# create listener, can use the `ethkey` value to close the socket later
key = await bsm.start_aggtrade_socket('BTCUSDT', handle_evt)

while True:
    print("doing a sleep")
    await asyncio.sleep(20, loop=loop)

if name == "main":

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

The API tries to reconnect, I see the following line in DEBUG mode: 'websocket btcusdt@aggTrade reconnecting 4 reconnects left'

but it never manages to reconnect and the loop continues to write "doing a sleep" with nothing else.

investing in the file websocket.py, it seems that the line

'asyncio.sleep(reconnect_wait)'

never finish.

Could you please inestigate and help me to fix this issue?

Thanks a lot, Cyrille.

Environment (please complete the following information):

CyrilleBoullier commented 4 years ago

Is the branch feature/asyncio still active with developments?

oliver-zehentleitner commented 4 years ago

Hello! I developed an async websocket lib for binance which reconnects automatic: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api Best regards, Oliver