Python: Socket and threads?

前端 未结 4 789
情书的邮戳
情书的邮戳 2020-12-10 04:43

I have a design problem:

I have two threads, a heartbeat/control thread and a messagehandler thread.

Both are sharing the same socket, however the messageHan

4条回答
  •  不思量自难忘°
    2020-12-10 05:08

    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.

提交回复
热议问题