'How to restart binance threaded websocket after losing internet connection

Is there a way to automatically restart binance ThreadedWebsocketManager when the internet connection was lost?



Solution 1:[1]

Surely, as python-binance sdk is listing it as one of its features:

Websocket handling with reconnection and multiplexed connections

Since they are an open-source project, maybe having a look at their source code be helpful.

P.S. As the question author asked for more info: I searched github repository of python-binance for the phrase reconnect and the first result pointed me to where you expect.

In their latest version(which now reads 0.7.10) they have defined a complete class for this purpose, namely class ReconnectingWebsocket based on python asyncio module. But in the version that i am accustomed to(0.7.5) they were using class ReconnectingClientFactory from the twisted package to accomplish this.

... And to see how they have used this class, Just search their repository recursively until arriving to the point that they start to use their stuff rather than defining more stuff!

In this case(Version 0.7.10) the next phrase to search for is ReconnectingWebsocket which brings up declaration of class KeepAliveWebsocket(ReconnectingWebsocket):. One more search to see what are they doing with KeepAliveWebsocket shows this line in the body of the method _get_account_socket of class BinanceSocketManager so every time you are calling get_account_socket you are implicitly implementing the functionality of ReconnectingWebsocket to enjoy auto reconnecting feature of python-binance.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer