473,231 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

SocketServer, its offspring, and threads

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
5 2256
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Hoarau | last post by:
Hello, I have build a client/server application with the socket module. The server mades UDP broadcasting and the client only reads UDP broadcast messages. All work fine. Now I want to use for...
3
by: Ergin Aytac | last post by:
I'm trying to run a script written in python and have some socket connection problems. I cutted the origin script (more than 1000 lines) so it is only the part of the connection and there is no...
0
by: Adil Hasan | last post by:
Hello Fred, I just ran across your question. I think that the following code will work: ----- SERVER CODE ------ import SocketServer import time class...
5
by: missiplicity | last post by:
Hi, I am newbie to Python language and am taking my baby steps. I am using Python2.4 from ActiveState on W2K. I am trying to create a simple SocketServer program. Just adding the following 2 lines...
12
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a...
3
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server...
12
by: rbt | last post by:
I have written a python socketServer program and I have a few questions that I hope the group can answer... here is a simple version of the server: class...
0
by: Tomi Hautakoski | last post by:
Hello, I'm a Python newbie trying to figure out how to use SocketServer with IPv6. I would like to set up a TCPServer working like below but how to tell SocketServer I need to use AF_INET6? ...
5
by: Joshua J. Kugler | last post by:
Considering that UNIX Network Programming, Vol 1 (by W. Richard Stevens) recommends "_All_ TCP servers should specify to allow the server to be restarted ," and that self.allow_reuse_address =...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.