473,881 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SimpleXMLRPCSer ver interruptable?

I'm coming back to Python after an absence and it's surprising how
many things I've forgotten since wandering (against my will) into Java
land.

Anyway, I have a need for a way to make SimpleXMLRPCSer ver
interruptable. Basically, I have a main server that, in response to
certain RPC calls, creates additional servers on different ports. I
then need to be able to shut these additional servers down.

I've got something like this in the __init__ of the class which
contains the spawned servers:

def __init__(self, host, port):
:
:
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while True:
server.handle_r equest()
except:
pass

One of the methods that I register is:

def shutdown(self):
raise "Quitting time"

Through watching logs and such I can see that the shutdown() method is
getting called, but the loop isn't getting broken. I've done
something like this before in a previous job (so I don't have my
source in front of me, more's the pity) and am hoping someone can
chime in with a good idea.

Thanks in advance!
Bret Wortman
Dec 5 '07 #1
9 3280
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:

def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break

This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....
On Dec 5, 2:00 pm, Bret <bret.wort...@g mail.comwrote:
I'm coming back to Python after an absence and it's surprising how
many things I've forgotten since wandering (against my will) into Java
land.

Anyway, I have a need for a way to make SimpleXMLRPCSer ver
interruptable. Basically, I have a main server that, in response to
certain RPC calls, creates additional servers on different ports. I
then need to be able to shut these additional servers down.

I've got something like this in the __init__ of the class which
contains the spawned servers:

def __init__(self, host, port):
:
:
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while True:
server.handle_r equest()
except:
pass

One of the methods that I register is:

def shutdown(self):
raise "Quitting time"

Through watching logs and such I can see that the shutdown() method is
getting called, but the loop isn't getting broken. I've done
something like this before in a previous job (so I don't have my
source in front of me, more's the pity) and am hoping someone can
chime in with a good idea.

Thanks in advance!

Bret Wortman
Dec 5 '07 #2
En Wed, 05 Dec 2007 18:20:35 -0300, Bret <br**********@g mail.comescribi ó:
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:

def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break

This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....
You could try setting a reasonable timeout on the listening socket; I
would override the server_bind method, calling self.socket.set timeout(xxx)
before calling the inherited method. I've never actually done it with a
SimpleXMLRPCSer ver, but *should* work. Don't use a very small timeout,
because it affects *all* subsequent operations.

--
Gabriel Genellina

Dec 6 '07 #3
On Dec 5, 6:22 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
En Wed, 05 Dec 2007 18:20:35 -0300, Bret <bret.wort...@g mail.comescribi ó:
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:
def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break
This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....

You could try setting a reasonable timeout on the listening socket; I
would override the server_bind method, calling self.socket.set timeout(xxx)
before calling the inherited method. I've never actually done it with a
SimpleXMLRPCSer ver, but *should* work. Don't use a very small timeout,
because it affects *all* subsequent operations.

--
Gabriel Genellina
Try this:

def __init__(self, host, port):
self.done = False
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while not self.done:
server.handle_r equest()
except:
pass

Your shutdown method becomes:

def shutdown(self):
self.done = True

HTH

-Edward Kozlowski
Dec 6 '07 #4
On Dec 5, 10:19 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 6:22 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
En Wed, 05 Dec 2007 18:20:35 -0300, Bret <bret.wort...@g mail.comescribi ó:
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:
def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break
This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....
You could try setting a reasonable timeout on the listening socket; I
would override the server_bind method, calling self.socket.set timeout(xxx)
before calling the inherited method. I've never actually done it with a
SimpleXMLRPCSer ver, but *should* work. Don't use a very small timeout,
because it affects *all* subsequent operations.
--
Gabriel Genellina

Try this:

def __init__(self, host, port):
self.done = False
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while not self.done:
server.handle_r equest()
except:
pass

Your shutdown method becomes:

def shutdown(self):
self.done = True

HTH

-Edward Kozlowski
Sorry about the first post, I shot from the hip and had to make a few
more modifications to make it 'working' code. The example below works
fine for me.

import SimpleXMLRPCSer ver

class myServer:
def __init__(self, host, port):
self.done = False
self.server = SimpleXMLRPCSer ver.SimpleXMLRP CServer((host,
port))

def shutdown(self):
self.done = True
return 0

def getName(self):
return "Hey, I'm Ed"

def serverWrapper(s elf):
self.server.reg ister_function( self.getName)
self.server.reg ister_function( self.shutdown)
try:
while not self.done:
self.server.han dle_request()
except:
pass
if __name__ == "__main__":
myServer('local host', 6058).serverWra pper()

----
>>s.getName()
"Hey, I'm Ed"
>>s.shutdown( )
0

Dec 6 '07 #5
On Dec 5, 10:00 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 10:19 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 6:22 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
En Wed, 05 Dec 2007 18:20:35 -0300, Bret <bret.wort...@g mail.comescribi ó:
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:
def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break
This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....
You could try setting a reasonable timeout on the listening socket; I
would override the server_bind method, calling self.socket.set timeout(xxx)
before calling the inherited method. I've never actually done it with a
SimpleXMLRPCSer ver, but *should* work. Don't use a very small timeout,
because it affects *all* subsequent operations.
--
Gabriel Genellina
Try this:
def __init__(self, host, port):
self.done = False
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while not self.done:
server.handle_r equest()
except:
pass
Your shutdown method becomes:
def shutdown(self):
self.done = True
HTH
-Edward Kozlowski

Sorry about the first post, I shot from the hip and had to make a few
more modifications to make it 'working' code. The example below works
fine for me.

import SimpleXMLRPCSer ver

class myServer:
def __init__(self, host, port):
self.done = False
self.server = SimpleXMLRPCSer ver.SimpleXMLRP CServer((host,
port))

def shutdown(self):
self.done = True
return 0

def getName(self):
return "Hey, I'm Ed"

def serverWrapper(s elf):
self.server.reg ister_function( self.getName)
self.server.reg ister_function( self.shutdown)
try:
while not self.done:
self.server.han dle_request()
except:
pass

if __name__ == "__main__":
myServer('local host', 6058).serverWra pper()

---->>s.getName()
"Hey, I'm Ed"
>s.shutdown()

0
Thanks to all! I'm now keeping a file of my own snippets in hardcopy
so I won't lose them next time I change jobs. :-)
Bret
Dec 6 '07 #6
On Dec 6, 7:04 am, Bret <bret.wort...@g mail.comwrote:
On Dec 5, 10:00 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 10:19 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 6:22 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
En Wed, 05 Dec 2007 18:20:35 -0300, Bret <bret.wort...@g mail.comescribi ó:
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:
def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break
This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....
You could try setting a reasonable timeout on the listening socket; I
would override the server_bind method, calling self.socket.set timeout(xxx)
before calling the inherited method. I've never actually done it with a
SimpleXMLRPCSer ver, but *should* work. Don't use a very small timeout,
because it affects *all* subsequent operations.
--
Gabriel Genellina
Try this:
def __init__(self, host, port):
self.done = False
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while not self.done:
server.handle_r equest()
except:
pass
Your shutdown method becomes:
def shutdown(self):
self.done = True
HTH
-Edward Kozlowski
Sorry about the first post, I shot from the hip and had to make a few
more modifications to make it 'working' code. The example below works
fine for me.
import SimpleXMLRPCSer ver
class myServer:
def __init__(self, host, port):
self.done = False
self.server = SimpleXMLRPCSer ver.SimpleXMLRP CServer((host,
port))
def shutdown(self):
self.done = True
return 0
def getName(self):
return "Hey, I'm Ed"
def serverWrapper(s elf):
self.server.reg ister_function( self.getName)
self.server.reg ister_function( self.shutdown)
try:
while not self.done:
self.server.han dle_request()
except:
pass
if __name__ == "__main__":
myServer('local host', 6058).serverWra pper()
---->>s.getName()
"Hey, I'm Ed"
>>s.shutdown( )
0

Thanks to all! I'm now keeping a file of my own snippets in hardcopy
so I won't lose them next time I change jobs. :-)

Bret
Oops, this actually misses part of the problem -- I need to construct
this server programmaticall y, so imbedding the call to start the
server in the "if __name__" construct doesn't work. If I start it
within the __init__, then the object that's creating it gets stuck
waiting for it to finish, which it never will. I need this to be
autonomous, which is why I was trying to do the start of the server in
a separate thread, but that doesn't seem to work either (that's what's
causing my process to need the second call; the thread completes the
loop and enters the next handle_request( ) call before the event or
boolean gets set).

Basically, I need a way for one server to start other autonomous
servers without waiting on them, and still retain the ability to shut
them down when their work is done.

Back to the keyboard. ;-)
Bret
Dec 6 '07 #7
On Dec 6, 7:04 am, Bret <bret.wort...@g mail.comwrote:
On Dec 5, 10:00 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 10:19 pm, Edward Kozlowski <ekozlows...@gm ail.comwrote:
On Dec 5, 6:22 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
En Wed, 05 Dec 2007 18:20:35 -0300, Bret <bret.wort...@g mail.comescribi ó:
I just tried changing this so that I now have a threading.Event ()
called self.done, which is set within the body of the shutdown()
method. The serverWrapper loop now looks like this:
def serverWrapper() :
while True:
server.handle_r equest()
if self.done.isSet ():
break
This works, but only if I follow the shutdown() rpc call a few seconds
later with ... something else to cause handle_request( ) to complete
again. Obviously, not quite the right approach....
You could try setting a reasonable timeout on the listening socket; I
would override the server_bind method, calling self.socket.set timeout(xxx)
before calling the inherited method. I've never actually done it with a
SimpleXMLRPCSer ver, but *should* work. Don't use a very small timeout,
because it affects *all* subsequent operations.
--
Gabriel Genellina
Try this:
def __init__(self, host, port):
self.done = False
server = SimpleXMLRPCSer ver((host, port))
:
: Bunch of server.register _function calls
:
def serverWrapper() :
try:
while not self.done:
server.handle_r equest()
except:
pass
Your shutdown method becomes:
def shutdown(self):
self.done = True
HTH
-Edward Kozlowski
Sorry about the first post, I shot from the hip and had to make a few
more modifications to make it 'working' code. The example below works
fine for me.
import SimpleXMLRPCSer ver
class myServer:
def __init__(self, host, port):
self.done = False
self.server = SimpleXMLRPCSer ver.SimpleXMLRP CServer((host,
port))
def shutdown(self):
self.done = True
return 0
def getName(self):
return "Hey, I'm Ed"
def serverWrapper(s elf):
self.server.reg ister_function( self.getName)
self.server.reg ister_function( self.shutdown)
try:
while not self.done:
self.server.han dle_request()
except:
pass
if __name__ == "__main__":
myServer('local host', 6058).serverWra pper()
---->>s.getName()
"Hey, I'm Ed"
>>s.shutdown( )
0

Thanks to all! I'm now keeping a file of my own snippets in hardcopy
so I won't lose them next time I change jobs. :-)

Bret
For completeness, what I ended up doing is this:

server = SimpleXMLRPCSer ver((host, port))
server.socket.s ettimeout(0.1)

ServerWrapper became:

def ServerWrapper() :
while True:
try:
server.handle_r equest()
if self.done.isSet ():
break
except timeout:
pass

And that seems to do the trick! Giving the socket a timeout allows me
to check the Event more frequently and catch the change within a
reasonable time.

Thanks again, Ed and Gabriel.
Bret
Dec 6 '07 #8
En Thu, 06 Dec 2007 13:11:09 -0300, Bret <br**********@g mail.comescribi ó:
For completeness, what I ended up doing is this:

server = SimpleXMLRPCSer ver((host, port))
server.socket.s ettimeout(0.1)

ServerWrapper became:

def ServerWrapper() :
while True:
try:
server.handle_r equest()
if self.done.isSet ():
break
except timeout:
pass

And that seems to do the trick! Giving the socket a timeout allows me
to check the Event more frequently and catch the change within a
reasonable time.
Remember that the timeout applies to *all* socket operations; what if the
XML request requires more than 0.1s to read?
If handling large requests is more important than the delay at shutdown
time, consider raising that value.

--
Gabriel Genellina

Dec 7 '07 #9
On Dec 6, 7:43 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
En Thu, 06 Dec 2007 13:11:09 -0300, Bret <bret.wort...@g mail.comescribi ó:
For completeness, what I ended up doing is this:
server = SimpleXMLRPCSer ver((host, port))
server.socket.s ettimeout(0.1)
ServerWrapper became:
def ServerWrapper() :
while True:
try:
server.handle_r equest()
if self.done.isSet ():
break
except timeout:
pass
And that seems to do the trick! Giving the socket a timeout allows me
to check the Event more frequently and catch the change within a
reasonable time.

Remember that the timeout applies to *all* socket operations; what if the
XML request requires more than 0.1s to read?
If handling large requests is more important than the delay at shutdown
time, consider raising that value.

--
Gabriel Genellina
Good point. Thanks!
Dec 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4811
by: Marco Aschwanden | last post by:
I would like to develop a server based on python's xmlrpc. But I realized that SimpleXMLRPCServer does not spawn a thread for each request. How could the SimpleXMLRPCServer be turned into a multi-threaded Server? Is there a reason why the SimpleXMLRPCServer is not multi-threaded? Is there a plan to make it multi-threaded? Thanks for any hints in advance,
3
3850
by: Maxim Khesin | last post by:
Hi, the typical usage of SimpleXMLRPCServer registers some class with the server instance and then jumps into a serve-forever loop, e.g. server = SimpleXMLRPCServer(('', 8000)) server.register_instance(MyClass()) server.serve_forever() is there a way to process actions other than XML-RPC
4
2344
by: codecraig | last post by:
Hi, I thought I posted this, but its been about 10min and hasnt shown up on the group. Basically I created a SimpleXMLRPCServer and when one of its methods gets called and it returns a response to the client, the server prints some info out to the console, such as, localhost - - "POST /RPC2 HTTP/1.0" 200 - Anyhow, is there a way I can surpress that so its not printed to the
0
1194
by: Thomas G. Apostolou | last post by:
Hello all, I use Python 2.3.3 and try to patch SimpleXMLRPCServer.py with the patch i got from Python.org. so after changing to the directory where both SimpleXMLRPCServer.py and SimpleXMLRPCServer.patch reside i run : patch -i SimpleXMLRPCServer.patch -b --verbose --dry-run SimpleXMLRPCServer.py and i get : Hmm...patch: **** unexpected end of hunk at line 47
0
2008
by: JDF | last post by:
I am trying to create a Windows service using SimpleXMLRPCServer and win32serviceutil. The service itself seems to be working properly (starts, stops, etc) and I can connect using an XMLRPC client from the localhost. However when I connect from a remote client, I either get a socket error or an xmlrpclib.ProtocolError error. If I use serve_forever() rather than handle_request(), the remote clients can connect but it breaks the Windows...
2
3138
by: Laszlo Nagy | last post by:
Hello, I ran in the same problem again. Many others have the same problem. Just Google for this: "SimpleXMLRPCServer allow_none site:python.org". Looks like the 'allow_none' patch was commited to trunk on 2005 Dec ( http://mail.python.org/pipermail/python-checkins/2005-December/048289.html ) I just upgraded to Python 2.4.3 (it was released on March 29, 2006) and SimpleXMLRPCServer.py still has the old code. I can work around this by...
0
1649
by: Jeremy Monnet | last post by:
Hello, I've started python a few weeks ago, and to now everything went fine with my cookbook and a learning book. Now, I've tried the SimpleXMLRPCServer, and it worked OK untill I tried to get the client IP address. I have searched a long time the Internet but couldn't find a _simple_ solution :-) #Code
0
1227
by: Juju | last post by:
Hi, First, sorry for my poor English ! I used the SimpleXMLRPCServer facility of Python to develop a multithread-server, here's part of my code : -- class TotoSimpleXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer):
3
7255
by: Achim Domma | last post by:
Hi, is SimpleXMLRPCServer multithreaded or how does it handle multiple clients? I want to implement a simple server which will be queried by multiple processes for work to be done. The server will simply hold a queue with files to process. The clients will ask for the next file. Do I have to sync access to the queue or is the server not threaded at all? regards,
0
11104
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10724
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9556
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.