Search code examples
pythonpython-3.xwebsocketpython-asyncioparallel-python

Opening multiple Websockets in parallel in Python


I am using the websocket library in Python and I am new to this.

I want to create multiple different connections to websockets. This happens through my custom WebsocketProcess class which opens the connection, receives the event, keeps a record Id and then calls an API to grab the information for this particular record.

I am having trouble running them in parallel.

Please, see below (ignore the numerous imports)

Main:

#if __name__ == "__main__":
async def main():

    #The length AccessTokens, ClientDescriptions and SQLTablesFix determines how many websockets we need to open
    L = await asyncio.gather(
        properties[0].websocket_starting(AccessTokens[0], ClientDescriptions[0], SQLTablesFix[0]),  
        properties[1].websocket_starting(AccessTokens[1], ClientDescriptions[1], SQLTablesFix[1]),
        ...
        ...
       )
    
asyncio.run(main())

The WebsocketProcess class is as follows:

class WebsocketProcess:
    """description of class"""

    def on_error(self, ws, error):
        #{Relevant Code Here}


    def on_open(self, ws):
        print("\nOn Open\n")
        def run(*args):
            while True:
                try:
                    time.sleep(1)
                except TimeoutError:
                    pass
            ws.close()

    def on_close(self):
        #{Relevant Code Here}


    def on_message(self, ws, message):
        #{Relevant Code Here}
        ws.close()


    def connect_websocket(self, AccessToken, ClientDescription, SQLTablesFix):
        ws = websocket.WebSocketApp("_______url_here_____",        
                                   on_open = self.on_open,
                                   on_message = self.on_message,
                                   on_error = self.on_error,
                                   on_close = self.on_close,
                                   cookie = "ClientToken=_______; AccessToken=%s" % AccessToken)
        ws.run_forever()



    async def websocket_starting(self, AccessToken, ClientDescription, SQLTablesFix):
        print("\nwebsocket_starting")
        self.AccessToken = AccessToken
        self.ClientDescription = ClientDescription
        self.SQLTablesFix = SQLTablesFix

        self.connect_websocket(self.AccessToken, self.ClientDescription, self.SQLTablesFix)

As you can see from the above, I have changed the main to asynchronous to run multiple instances of the websocket_process class in parallel. It opens a connection to the first websocket and it stops there waiting for events, without proceeding to open a second websocket.

I tried making the WebsocketProcess class entirely asynchronous but the errors I am receiving an error specifying that coroutine 'run' was never awaited (in the connect_websocket method).

Do you guys have any suggestions on how to run multiple instances of the websocket_process class in parallel?

Thanks!


Solution

  • Your websocket operations are blocking operations, to use websocket in asyncio, use other async libraries like websockets, Tornado