Skip to content
Advertisement

Python Websocket-Client Package Frequent Ping-Pong Timeouts Upon Too Many Subscriptions

I am using the python websocket-client package (https://pypi.org/project/websocket-client/) to subscribe to multiple websocket channels. I implemented the basic ping-pong logic using the websocket client package as below. The ping-pong logic is working mostly fine under normal circumstances and I am able to run the code for hours without many disconnect/reconnect instances.

However, when I try to increase the number of channels I subscribe to, the program will encounter many “websocket._exceptions.WebSocketTimeoutException: ping/pong timed out” errors. It looks like the websocket package is not sending the ‘ping’ messages properly when they are too busy. Will get even more timed out errors when I try to add even more subscriptions.

Maximum stable websocket connection I could establish is around 4 separate websocket threads to different places with ~12 subscriptions each. Anything more than that will bring many timed out exception. I wonder if there is anything I could do to cater this issue or this is the limit of the websocket-client package/ my computer? Does anyone else encountered this problem before? Thanks.

Tried to set the ‘skip_utf8_validation’ to True to try to enhance the performance but it did not help.

   def run(self):
        # Setup the thread running WebSocketApp.
        wst = threading.Thread(target=self._run, name='{}Raw'.format(self.name))
        wst.daemon = True
        wst.start()

        connected = self.check_connected()
        self.post_connect(connected)

   def _run(self):
            self.ws.run_forever(
                ping_interval=20,
                ping_timeout=10,
                ping_payload=self.ping_payload,
                skip_utf8_validation=True,
            )


error: Traceback (most recent call last):
  File "~/lib/python3.10/site-packages/websocket/_app.py", line 383, in run_forever
    dispatcher.read(self.sock.sock, read, check)
  File "~/lib/python3.10/site-packages/websocket/_app.py", line 68, in read
    check_callback()
  File "~/lib/python3.10/site-packages/websocket/_app.py", line 380, in check
    raise WebSocketTimeoutException("ping/pong timed out")
websocket._exceptions.WebSocketTimeoutException: ping/pong timed out

Advertisement

Answer

Found one possible cause – one of the websocket threads is having some codes that are running slow and making all other websocket threads slower and slower while running. Trying to fix and see if that is the root clause. Thanks guys first.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement