socketserver question

  • Thread starter K Richard Pixley
  • Start date
K

K Richard Pixley

Once I've instantiated my server class, along with a handler class,
called server.serve_forever(), handler.handle() has been called, I've
done my work, and I'm ready to shut the whole thing down...

How do I do that?

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.

I've also tried sys.exit() but it seems that the server object is
catching that as an exception.

How is this expected to work? How do I terminate the
server.serve_forever() loop?

--rich
 
B

Bryan

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, ())
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top