You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I successfully created a python 3.5 websocket server with the following code, but I'm now struggling to close the connection after n missed pings. How is this possible?
You shouldn't be using threads. Use an asyncio Task instead. (Besides being correct advice in general — you lose most advantages of asyncio when you mix it with threads — this is also an answer to your question, because communicating with an event loop from another thread causes complications.)
Then, in that task, await asyncio.wait_for(conn.ping(), timeout=10); a timeout exception indicates a timeout.
Create a Future to use as a flag for exiting. When you decide the connection is down, set that Future to some value.
In your main loop, wait for both conn.recv() and that flag in parallel with asyncio.wait. If the flag is set, break from the loop. The handler will then return, which will cause the connection to be closed (uncleanly if the connection is indeed broken).
An easier way to do this is to serialize the two operations instead of parallelizing them. await asyncio.wait_for(conn.recv(), timeout=...); if you get a a timeout, await asyncio.wait_for(conn.ping(), timeout=...); if you get a timeout, assume the connection is broken and exit, else, go back to step 1.
I successfully created a python 3.5 websocket server with the following code, but I'm now struggling to close the connection after n missed pings. How is this possible?
The text was updated successfully, but these errors were encountered: