SimpleXMLRPCServer interruptable?

Bret bret.wortman at gmail.com
Thu Dec 6 11:11:09 EST 2007


On Dec 6, 7:04 am, Bret <bret.wort... at gmail.com> wrote:
> On Dec 5, 10:00 pm, Edward Kozlowski <ekozlows... at gmail.com> wrote:
>
>
>
> > On Dec 5, 10:19 pm, Edward Kozlowski <ekozlows... at gmail.com> wrote:
>
> > > On Dec 5, 6:22 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>
> > > > En Wed, 05 Dec 2007 18:20:35 -0300, Bret <bret.wort... at gmail.com> escribió:
>
> > > > > 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_request()
> > > > >         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.settimeout(xxx)
> > > > before calling the inherited method. I've never actually done it with a
> > > > SimpleXMLRPCServer, 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 = SimpleXMLRPCServer((host, port))
> > >     :
> > >     : Bunch of server.register_function calls
> > >     :
> > >     def serverWrapper():
> > >         try:
> > >             while not self.done:
> > >                 server.handle_request()
> > >         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 SimpleXMLRPCServer
>
> > class myServer:
> >     def __init__(self, host, port):
> >         self.done = False
> >         self.server = SimpleXMLRPCServer.SimpleXMLRPCServer((host,
> > port))
>
> >     def shutdown(self):
> >         self.done = True
> >         return 0
>
> >     def getName(self):
> >         return "Hey, I'm Ed"
>
> >     def serverWrapper(self):
> >         self.server.register_function(self.getName)
> >         self.server.register_function(self.shutdown)
> >         try:
> >             while not self.done:
> >                 self.server.handle_request()
> >         except:
> >             pass
>
> > if __name__ == "__main__":
> >     myServer('localhost', 6058).serverWrapper()
>
> > ---->>> 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 = SimpleXMLRPCServer((host, port))
    server.socket.settimeout(0.1)

ServerWrapper became:

    def ServerWrapper():
        while True:
            try:
                server.handle_request()
                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



More information about the Python-list mailing list