signal handling oddity

Moshe Zadka m at moshez.org
Mon Jun 9 10:22:15 EDT 2003


On Mon, 09 Jun 2003, "Justin Johnson" <justinjohnson at fastmail.fm> wrote:

> I tried adding code to
> handle SIGINT (below).  What happens now is, I control-c and nothing
> happens, but when the next xmlrpc request comes through, the service
> exits, running stopService.  So the control-c is caught, but only when
> the next request comes through.  Anyone know why this is?

You didn't bother to mention which platform it is. In any case, the
problem is due to some interaction between what Python does to make
sure your signal handling is safe and how the OS works. I can never
remember the exact reason, but it boils down to signals not interrupting
blocking system calls. The solution, of course, is not to block. If
SimeplXMLRPCServer uses what I think it does (an accept() call)
then there is no easy way to solve it.

At the risk of annoying lots of people here, I would suggest using
the Twisted XML-RPC server support. Twisted on Windows (where IIRC
this problem manifests itself) will run select() for at most 5-second
delays, so a C-c would have at most 5 second delays. If even that
is too much,

def foo():
    reactor.callLater(1, foo)
foo()

should make sure it caps at 1 second, at the cost of a slight performance
penalty.

Further reading:
Using XML-RPC: http://twistedmatrix.com/documents/howto/xmlrpc
Setting up the web server:
http://twistedmatrix.com/documents/howto/using-twistedweb
-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/





More information about the Python-list mailing list