473,287 Members | 1,834 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,287 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 2257
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: 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:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.