views:

108

answers:

2

I'm trying to find a good and simple method to signal child processes (created through SocketServer with ForkingMixIn) from the parent process.

While Unix signals could be used, I want to avoid them since only children who are interested should receive the signal, and it would be overkill and complicated to require some kind of registration mechanism to identify to the parent process who is interested.

(Please don't suggest threads, as this particular program won't work with threads, and thus has to use forks.)

+1  A: 

I have come up with the idea of using a pipe file descriptor that the parent could write and then read/flush in combination with select, but this doesn't really qualify as a very elegant design.

In more detail: The parent would create a pipe, the subprocesses would inherit it, the parent process would write to the pipe, thereby waking up any subprocess select():ing on the file descriptor, but the parent would then immediately read from the read end of the pipe and empty it - the only effect being that those child processes that were select():ing on the pipe have woken up.

As I said, this feels odd and ugly, but I haven't found anything really better yet.

Edit:

It turns out that this doesn't work - some child processes are woken up and some aren't. I've resorted to using a Condition from the multiprocessing module.

Teddy
+2  A: 

Since you are on a unix system, semaphores should be the easy answer. Unfortunately, python does not seem to offer a way to call the semop system call.

If you are using python 2.6 , you may be able to use the multiprocessing module Condition class.

Mark Borgerding