SIGCHLD handle & socket

Donn Cave donn at u.washington.edu
Fri Dec 8 14:46:36 EST 2000


Quoth redjade at my-deja.com:
| I am writing a forking-type server with python.
| My plan is:
| 1) first, open a socket with port 15151
| 2) if there's a connection request, fork a child
| 3) child will die after 3 sec of connection establishment.
| 4) parent should handle SIGCHLD signal so that it make zombie-child
| disappear.
| 5) parent should allow any connection request and should be fork-able.

As another followup already mentions, an interrupt exception is
inevitable in this scheme.  In C, you'd check for errno == EINTR
after every error return from a socket, pipe or tty I/O function
and just retry.  In Python, you have to trap an exception.

If it's at all possible, you will have better luck if you don't
trap SIGCHLD and find some other way to wait() for the child
processes.  If you don't trap this unusual signal, it won't be
delivered and you won't have those interrupts.  Even in C,
they're rarely worth the hassle, and with Python's extra signal
handling issues plus the exception business, surely not worth it.

If you want to take all the exited child processes on a regular
schedule, you can time out on the bound socket, for example -
use select() to check for connection ready to accept() or time out
after some fraction of a second.  Use posix.waitpid(-1, posix.WNOHANG)
to check for any and all outstanding exited processes.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list