SocketServer, its offspring, and threads

eliben eliben at gmail.com
Sun May 25 11:40:26 EDT 2008


Hello,

I have a small wxPython application. Today I was trying to add some
RPC capability to it, so I implemented an instance of
SimpleXMLRPCServer that runs in a separate thread when invoked and
answers requests.

All went fine until I realized that I have to sometimes stop the
server - which is where I ran into a problem. Python threads can not
be killed after they've been started. They can be kindly requested to
end, but it's up to the thread itself to decide when it wants to
finish. At least this is my understanding. It probably makes sense,
because threads can hold resources which can't just be dropped
suddenly, and have to be cleaned in a proper manner. (Does this sound
like the truth ?)

Anyway, this creates a problem because SimpleXMLRPCServer likes to
block and never return. I dug deeper and found out that all offspring
of SocketServer can only handle requests in a blocking manner. Even if
you call handle_request( ) and not serve_forever(), it will block
until a request is received and handled. This means that a
SocketServer object running in a thread blocks the thread, and this
thread can not be stopped.

Is there any graceful solution to this problem ?

I suppose I can use sockets in non-blocking mode, or use select(), but
this will mean I will have to implement my server with raw sockets and
not a handy helper like SocketServer. For the XML-RPC server I want
this is a headache, as I will probably have to implement my own XML-
RPC server based on raw non-blocking sockets.

Thanks in advance for any ideas and suggestions
Eli



More information about the Python-list mailing list