socketserver question

Bryan bryanjugglercryptographer at
Sun Jan 8 06:11:14 EST 2012

K Richard Pixley wrote:
> The doc says server.shutdown(), but if I call self.server.shutdown()
> from within handler.handle(), I seem to get a deadlock, which is exactly
> what I'd expect in a single threaded system with no way to "signal" the
> server.server_forever() loop which is several frames up the stack.

Well, it's documented behavior, but not what I would have expected.
Seems to go out of its way to deadlock. Stopping the main loop from
inside a request is perfectly reasonable thing to want to do, and
single-threaded GUI's do it all the time.

It's not an up-the-stack problem. shutdown() sets a flag that will
cause the loop not to repeat, but then calls wait() for an event that
won't be signaled until the loop exits, breaking the single-threaded
case. It's bad design.

Given the library class is as it is, one solution (untested) is to
relax being single-threader for just a bit.

    import thread
    thread.start_new_thread(server.shutdown, ())


More information about the Python-list mailing list