How to kill a SocketServer?

Julian Smith jules at REMOVETHIS.op59.net
Mon Apr 12 18:18:40 EDT 2004


On Mon, 12 Apr 2004 17:29:35 -0400
Peter Hansen <peter at engcorp.com> wrote:

> Jean-Pierre Bergamin wrote:
> > The accept() call still won't get interrupted. :-(
> > 
> > Other ideas?
> 
> You have three choices.
> 
> 1. Run your server as a separate process, communicating with it
> via some kind of RPC, and just kill it when desired.
> 
> 2. Use non-blocking sockets.  This is the standard and
> simplest approach in many ways.
> 
> 3. Arrange to have one thread open a socket connection to the
> application, waking up your accept()ing thread.  Then check
> a flag which tells the server thread to exit.

I have a class that can be constructed from a socket and looks like a file
object, but whose blocking read() method can be interrupted by a different
thread. The read() method uses poll() to block on both the real underlying
file descriptor and an internal file descriptor created using os.pipe().

It works on OpenBSD and Cygwin, but I haven't tried it on anything else yet.
I'm a relative newcomer to Python, so I'm sure there are some subleties that
I've missed.

See http://www.op59.net/cancelable.py if you're interested.

> 
> By definition, blocking calls block, and you can't safely kill
> a thread in Python so pick one from the above and run with it...
> 
> -Peter


- Julian

-- 
http://www.op59.net



More information about the Python-list mailing list