3

Websockets in python Quart with multiple workers?

Is there a solution to broadcast a message when my app is launched across multiple workers - for example using hypercorn?

I'm currently launching my app like this:

$ hypercorn -b 127.0.0.1:8000 -w 4 wsgi:app

But the documented solution is to keep the connected clients in memory, with no way to broadcast to others that subscribed to the same event.

Any ideas? I was thinking to use Redis to achieve this, but I would like a simpler solution.

LightCC
  • 9,804
  • 5
  • 52
  • 92
go-pera
  • 31
  • 2
  • Did you solve this? – Thanasis Mattas Aug 10 '22 at 06:57
  • 1
    Yes, I solved it. I'm using Redis and PUB/SUB to send messages. Every time a user creates a WebSocket, I'm subscribing to which events the user is expecting. Then, when another process publishes a message that another process is subscribed to, it will receive it and send it to the FE. Some key words: __asyncio.Queue()__, __redis PUB/SUB__, __Quart web sockets__ – go-pera Aug 12 '22 at 13:38

1 Answers1

1

I don't think there is a simpler solution than Redis (or similar third party tool) for this.

It is possible to run Hypercorn with memory shared across the workers, but it isn't simple to setup. Start here if you want to try this. At some point though you may have workers across multiple machines, in which case shared memory no longer works.

pgjones
  • 6,044
  • 1
  • 14
  • 12