Problem with Multi Threaded Python App and Socket Connections

Python: Socket and threads?

Unfortunately,The socket shared by multi-thread is not thread safe.Think about buffer two threads operate on with no lock.

The normal way to implement is with two socket,just like what ftp does.cmd socket and msg socket.

If you wanna implement this by one socket,you can put different type msgs into different queues,and with a third thread consumes the queue and send them through the only socket.

In this way,you can control heartbeat msg priory to data msg.

Python - multithreaded sockets

Your understanding of how Threads work in Python seems to be incorrect, based on the question you are asking. If used correctly, threads will not be blocking: you can instantiate multiple thread with Python. The limitation is that, due to the Global Interpreter Lock (GIL), you cannot get the full parallelism expected in thread programming (e.g. simultaneous execution and thus, reduced runtime).
What is going to happen in your case is that the two threads will take, together, the same amount of time that they would take if they were executed sequentially (although that is not necessarily what happens in practice).



Related Topics



Leave a reply



Submit