Python - Cannot serve multiple clients at the same time using Websockets

Viewed 21

I have a stuff to de-serialize and transmit data between two programs

Now I have a problem is that the handler for the daemon will be "Inactive" or "Freezed" after the client connects, everything inside it will stop and only the handler for client works.

import asyncio
import websockets
import concurrent
import sdk_exports

class Server:
    def __init__(self):
        self.msgs = asyncio.Queue(128)
    
    // Server for Daemon
    async def payload_recv(self, ws, path):
        while ws.open:
            payload = await ws.recv()
            self.msgs.put(payload)

    // Server for Client
    async def ws_evt(self, ws, _):
        while ws.open:
            if not self.msgs.empty():
                payload = self.msgs.get()
                await ws.send(sdk_exports.handle(payload))

async def main():
    srv = Server()
    ws_srv = await websockets.serve(srv.ws_evt, '127.0.0.1', '8328', ping_interval=None)
    pl_rcv = await websockets.serve(srv.payload_recv, '127.0.0.1',
                         '28468', ping_interval=None)
    print("Standby. Waiting for connections...")
    await asyncio.gather(ws_srv.wait_closed(), pl_rcv.wait_closed())


if __name__ == "__main__":
    asyncio.run(main())
0 Answers
Related