Windows XMLRPC Service
half.italian at gmail.com
half.italian at gmail.com
Tue Jun 19 02:45:19 EDT 2007
On Jun 18, 2:16 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Mon, 18 Jun 2007 00:25:25 -0300, <half.ital... at gmail.com> escribió:
>
>
>
>
>
> > I'm trying to serve up a simple XMLRPC server as a windows service. I
> > got it to run properly, I'm just not sure how to stop it properly.
> > Most of the documentation/examples I found for this was from forums,
> > so I'd love some links to relevant info also. Here's what I
> > have...taken from the cookbook with the xmlrpc server added:
>
> > def __init__(self, args):
> > win32serviceutil.ServiceFramework.__init__(self, args)
> > # Create an event which we will use to wait on.
> > # The "service stop" request will set this event.
> > self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
>
> > def SvcStop(self):
> > # Before we do anything, tell the SCM we are starting the stop
> > process.
> > self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
>
> > # quit the xmlrpc sever
> > self.server.quit()
>
> What is quit()? As the server may be processing a request I'd move any
> finalization code below, after exiting the while loop.
>
>
>
> > # And set my event.
> > win32event.SetEvent(self.hWaitStop)
>
> > def SvcDoRun(self):
> > # Serve up the XMLRPC forever
> > self.server =
> > SimpleXMLRPCServer.SimpleXMLRPCServer(("10.0.1.6", 8000))
> > self.server.register_instance(MyClass())
> > self.server.serve_forever()
>
> > win32event.WaitForSingleObject(self.hWaitStop)
>
> The simplest solution is to replace serve_forever with a loop waiting on
> hWaitStop:
>
> while WaitForSingleObject(self.hWaitStop, 0)==WAIT_TIMEOUT:
> self.server.handle_request()
>
> Set the socket timeout to a reasonable value (you'll have to wait that
> time before exiting). Also, a ThreadingTCPServer may be better if you
> expect more than a request at a time. If you search past messages you may
> find other ways.
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -
I can't quite figure out where to set the "socket timeout". I tried
setting win32event.WAIT_TIMEOUT, but I'm pretty sure that's not the
variable you were talking about. I did manage to make it multi-
threaded by incorporating a different recipe, and I'm beginning to
understand the control flow a bit better, but it doesn't seem to be
doing what I expect. When SvcStop() is executed and calls
win32event.SetEvent(self.hWaitStop), the while loop should break as
win32event.WaitForSingleObject(self.hWaitStop, 0) returns zero at this
point. But it doesn't do that. What am I missing?
import win32serviceutil
import win32service
import win32event
import SocketServer
from SimpleXMLRPCServer import
SimpleXMLRPCServer,SimpleXMLRPCRequestHandler
# Threaded mix-in
class
AsyncXMLRPCServer(SocketServer.ThreadingMixIn,SimpleXMLRPCServer):
pass
class MyClass(object):
def hello(self):
return "Hello World"
class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "PythonXMLRPC"
_svc_display_name_ = "PythonXMLRPC"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# Create an event which we will use to wait on.
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
import socket
localhost = socket.gethostbyname(socket.gethostname())
self.server = AsyncXMLRPCServer((localhost, 8000),
SimpleXMLRPCRequestHandler)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
#print "EVENT:",
win32event.WaitForSingleObject(self.hWaitStop, 0) # returns 0 here
def SvcDoRun(self):
self.server.register_instance(MyClass())
#win32event.WAIT_TIMEOUT = 2 --- This just makes the loop
never execute because
# the WaitFor... part always returns 258
while win32event.WaitForSingleObject(self.hWaitStop, 0) ==
win32event.WAIT_TIMEOUT:
self.server.handle_request()
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService)
Thanks for any help!
~Sean
More information about the Python-list
mailing list