Closed suryanarayanan035 closed 3 months ago
So, I thought of moving the pinging work to a separate worker. So my main server, will handle the connections and my worker will do the pinging. To do so, I have to serialize/pickle the websocket object which is not possible.
"worker" in this context is "asyncio coroutine". Coroutines share the same memory space. Therefore, you don't need to serialize/pickle.
(Besides, you cannot (easily) share sockets between processes. Therefore, generally speaking, serialize/pickle a network connection with the intent of sharing it never makes sense. If it's the same process, you don't need to serialize. If it's in a different process, you cannot share.)
Are you aware the websockets already does this for you and you can simply tune the parameters as you wish?
e.g. serve(ping_interval=10, ping_timeout=5)
if you think that latency > 5s means the connection is dead
Scenario
Hello there, Thanks for creating this cool and elegant library. I am building a chat application using this library. In my application I want to ping the client for every 10 seconds to verify whether they are connected still. But the issue is I cannot do so.
What I have tried
I tried to create an async function which will sleep for 10 seconds then call websockets.ping method. But this can't be used because if I use
asyncio.sleep
then I need to useawait
which will block the codes below the function call.Expectation
So, I thought of moving the pinging work to a separate worker. So my main server, will handle the connections and my worker will do the pinging. To do so, I have to serialize/pickle the websocket object which is not possible.
So, I would like to know a way to scale websockets with multiple worker or running interval based tasks without blocking the main process.
Here is my server code:
Happy to provide more information if needed. Thanks in adavance!