Threads, signals and sockets (on UNIX)

fumanchu fumanchu at
Mon Jun 11 19:34:41 CEST 2007

On Jun 11, 3:34 am, geoffbache < at> wrote:
> I have a Python program (on UNIX) whose main job is to listen on a
> socket, for which I use the SocketServer module. However, I would also
> like it to be sensitive to signals received, which it isn't if it's
> listening on the socket. ("signals can only be received between atomic
> actions of the python interpreter", presumably - and control will not
> return to Python unless something appears on the socket). Does anyone
> have a tip of a good way to do this?
> I can of course put the SocketServer in a thread and call
> signal.pause() in the main thread, but this falls down when the
> SocketServer terminates normally, as the signal.pause() cannot be
> interrupted except via signals. So I tried sending a "dummy" signal
> (SIGCHLD) from the thread when the SocketServer terminates, which
> seems to work on Linux but not Solaris. And which in any case feels a
> bit hackish - surely there has to be a better way?

Use a timeout on your socket and put socket.accept() in a loop:


while True:
        childsocket, addr = mainsocket.accept()
        handle(childsocket, addr)
    except socket.timeout:

Robert Brewer
System Architect
Amor Ministries
fumanchu at

More information about the Python-list mailing list