How to share DATA between multi worker

I have a websocket service,for collect ws clients and then send msg to these clients,
I use Queue (app.quename=collections.deque()) collects the websocket client,
The Issue is:when i run sanic app with worker>1,some of the msgs received by the client lost.
And then I id() the quename,between the different workers are the same.

That makes sense because they are separate processes. Sharing data between processes can be tricky, but I have found one of the best solutions (happy to share others) is to use a pub/sub.

Here is a working example: https://gist.github.com/ahopkins/9816b39aedb2d409ef8d1b85f62e8bec

Thanks a lot for your suggestion.

1 Like

Do you have any benchmark numbers on using pubsub vs not? Id be pretty interested in seeing that.

What is the alternative? Polling?

I’ve been incredibly happy by separating the data which needs to be shared between the multiple workers to a different micro service, in charge of the shared data, with only one worker.

Once I made the connection between each worker and the other microservice kept alive for ever (a year), they perform quite better than when all of it was in the same service and I have no more issues with sharing data. In a sense I made the multi worker service purely functional, with no shared state.

Of course, I am not sure this can be applied in this case.

1 Like

Sharing state between workers is probably the most asked question I see. New docs will cover some code examples of different strategies. If you’d like to share a basic version of what you are doing, I’d be happy to include it.

2 Likes

FYI, I am working on a side project I’ll probably release later in the year to sync multiple workers using the new Signals api I am working. Stay tuned.

2 Likes

Awh I was really excited to see some super sketchy methods to achieve this goal