Search code examples
pythonmultithreadingwebsocketpython-multithreadinggil

If I am listening to a websocket in one thread and running a function in another thread is it possible to miss messages


Title says it all really. I am running a program on a Linux EC2 instance with 4 threads. Three of these are listening to different websockets and the final one is webscraping and calling off a set of other functions when needed.

Is it possible that if the GIL is owned by the 4th thread (i.e it is currently running its calculation through the single core) that websocket messages could be 'missed' by the threads listening?

I am beginning to think it isn't possible, but have no understanding as to why. I have looked around, but to little avail.


Solution

  • Not really, even if your application is completely blocked say by scheduling or simply sleeping the operating system will queue the incoming network messages. You might lose messages say if the TCP buffer starts to overflow, I reckon that is unlikely in your case. You can test your idea by deliberately sleeping in the 4th thread for some time and see if messages are dropped.