[zeromq-dev] Poll on sockets OR child process

Greg Ward greg at gerg.ca
Thu Nov 28 01:58:12 CET 2013

On 27 November 2013, Michael Haberler said:
> > Is there any sane way to do this? (The whole system is written in
> > Python, using zeromq-3.2.3 and pyzmq 13.1.0.)
> maybe you'd want to use SIGCHLD

Hey, 20 years with Unix and I'm still looking for an excuse to use
SIGCHLD. (So far I've always been happy with waitpid() and friends.)
You might have just found it for me -- thanks! 

> and poll on a signalfd

Hmmm, I had not heard of that. Sounds interesting. Downsides: it's
Linux-specific, and AFAICT is not exposed via the Python std library.
(There's a binding here: https://launchpad.net/python-signalfd.)

My initial inclination is to handle SIGCHLD and ... ummm ... then
what? Say my code looks like this:

  def handle_sigchld(sig, frame):
      print('child terminated')
      [...now what?]

  def mainloop():
      [install handle_sigchld() for SIGCHLD]
      while True:
          sockets = poller.poll(timeout)
	  [recv() messages, do stuff in response, send() replies, etc.]

Presumably that SIGCHLD will arrive while my mainloop() is inside
poller.poll(), which boils down to 0MQ being in a poll() system call.
So I'm guessing that poll() call will be interrupted, and my main loop
will be back in control. I guess the thing to do is set a "some child
terminated" flag in handle_sigchld(), and then check that flag when
poll() exits. That should let me get the results of the child process
back to the master process with minimal latency.

Unless, of course, 0MQ does clever stuff with signals and/or
interrupted system calls. I'm going to give this a shot and see what



More information about the zeromq-dev mailing list