[Baypiggies] Advice on multithreading socket architecture in Python

Fahrzin Hemmati fahhem at google.com
Thu Apr 7 18:12:45 EDT 2016


What are you using to send data to these two threads? Most mechanisms support a
timeout-based read, so you could put the heartbeat limit as the timeout and send
a heartbeat. Here's an example that uses Queue()s:
class SocketThreadForHardware(threading.Thread): def __init__(self, connection_info): … self.socket = socket.socket(…) self.write_queue = Queue.Queue()
# I'm ignoring read/recv here
def write(self, data): self.write_queue.put(data)
def run(self): while True: try: data = self.write_queue.get(block=True, timeout=HEARTBEAT_TIMEOUT_S) except Queue.Empty: data = HEARTBEAT self.socket.write(data)

On Thu, Apr 7, 2016 4:44 AM, Braun Brelin bbrelin at gmail.com wrote:
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/250c8d6b/attachment.html>


More information about the Baypiggies mailing list