[Baypiggies] Advice on multithreading socket architecture in Python

Braun Brelin bbrelin at gmail.com
Thu Apr 7 07:44:17 EDT 2016


Hi all,

I could use some advice on a program I'm writing in Python 3.

Basically, I have a hardware device which supports two socket connections.
I was planning on creating threads to do reads and writes to each socket as
necessary.  However, the hardware device will close the socket connection
after 120 seconds of inactivity so I need to send a heartbeat message to
the sockets in order to keep them open.

I'm trying to figure out the best way to do this.  My initial thought was
to create four threads, one for the 'control', i.e. send the heartbeat and
one for 'data'.  Basically similar to how ftp works with TCP ports 20 and
21.

This seems clunky, however. Since I now have to implement locking for each
socket as well as having four threads rather than two.

Is there a good way of having the threads become daemons, and somehow check
to see if they've sent any data over some preset timeout value and if not,
send a heartbeat to the socket?

Thanks

Braun Brelin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20160407/32e3bf37/attachment-0001.html>


More information about the Baypiggies mailing list