By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

SocketServer, its offspring, and threads

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On May 25, 5:40 pm, eliben <eli...@gmail.comwrote:
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

I ended up using an ugly hack to make it work for me. Since
handle_request() only blocks until a request is received, the thread
can be unblocked by sending it a real message. So to stop a server, I
opened a XML-RPC client connection (using ServerProxy from xmlrpclib)
and made a request. In the server thread, handle_process() returned
and the thread could see the flag requesting it to stop.

There must be a better way ! Any ideas ?

Eli
Jun 27 '08 #2

P: n/a
On 25 Mag, 17:40, eliben <eli...@gmail.comwrote:
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
A "shutdown" method has been added to SocketServer.
Since it will be available in Python 2.6 and 3.0 you could look at the
patch and include a modified version of SocketServer into your package
and use it:
http://bugs.python.org/issue1193577
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Jun 27 '08 #3

P: n/a
On May 26, 7:30 pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
On 25 Mag, 17:40, eliben <eli...@gmail.comwrote:
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

A "shutdown" method has been added to SocketServer.
Since it will be available in Python 2.6 and 3.0 you could look at the
patch and include a modified version of SocketServer into your package
and use it:http://bugs.python.org/issue1193577
This is what I was looking for, thanks.
Jun 27 '08 #4

P: n/a
eliben <el****@gmail.comwrote:
>
I ended up using an ugly hack to make it work for me. Since
handle_request() only blocks until a request is received, the thread
can be unblocked by sending it a real message. So to stop a server, I
opened a XML-RPC client connection (using ServerProxy from xmlrpclib)
and made a request. In the server thread, handle_process() returned
and the thread could see the flag requesting it to stop.

There must be a better way ! Any ideas ?
Well, maybe it is a matter of personal preference, but I don't see anything
wrong with this approach. If a thread is blocked waiting for a command,
what's wrong with sending it a "please commit suicide" command?
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #5

P: n/a
On May 25, 10:40*am, eliben <eli...@gmail.comwrote:
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.

Why not use setDaemon( true ) on the thread running the XMLRPC
server. When the main Python thread exits, it doesn't wait for so-
called "daemon" threads and just kills them.

Check out the "threading" module in the lib documentation

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.