kucoin-python-sdk icon indicating copy to clipboard operation
kucoin-python-sdk copied to clipboard

multiple errors with websocket

Open itayl2 opened this issue 1 year ago • 1 comments

Hi,

I love this package, thank you so much.

I am running into two issues with the websocket connection. This is my code:

async def main(loop):
    async def deal_msg(msg):
        if msg['topic'] == '/market/ticker:all':
            my_ticker_callback(Platform.KUCOIN, [msg])

    client = WsToken()
    ws_client = await KucoinWsClient.create(loop, client, deal_msg, private=False)
    await ws_client.subscribe('/market/ticker:all')
    while True:
        await asyncio.sleep(30)


def handler():
    while True:
        try:
            loop = asyncio.get_event_loop()
            loop.run_until_complete(main(loop))
        except Exception as e:
            logger.exception(f"Websocket failed")
            sleep(0.1)

When I use the code above, I run into this error every short while:

<Task finished name='Task-4' coro=<ConnectWebsocket._run() done, defined at ...\site-packages\kucoin\websocket\websocket.py:33> exception=ConnectionClosedError(None, None, None)> got an exception no close frame received or sent
_reconnect over.

When I change the sleep to 30, I get this every short while:

<Task finished name='Task-4' coro=<ConnectWebsocket._run() done, defined at ...\site-packages\kucoin\websocket\websocket.py:33> exception=ConnectionClosedError(None, None, None)> got an exception sent 1011 (unexpected error) keepalive ping timeout; no close frame received
_reconnect over.

Any idea how this can be solved?

itayl2 avatar Jun 08 '23 14:06 itayl2

You don't need the second while True loop. You can put that whole try-except block under if __name__ == '__main__' and run it like that.

quantokenizer avatar Dec 05 '23 19:12 quantokenizer

Because run_until_complete is a method that runs the event loop until a Future is done and returns the Future's result or raises its exception, you don't need to add a while True loop. Please refer to this and modify your code accordingly.

example_default_ws_public.py

ISAAC-XXYYZZ avatar Jul 29 '24 08:07 ISAAC-XXYYZZ