SimpleXMLRPCServer and Threading

Erik Johnson nobody at invalid.com
Wed Mar 28 19:19:30 EDT 2007


"Achim Domma" <domma at procoders.net> wrote in message
news:460acd70$0$20282$9b4e6d93 at newsspool3.arcor-online.net...
> 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?


    I don't claim to know much about the internals of the module, but it
imports BaseHTTPServer which I am sure is multithreaded.  I did not test
this with functions, but when you register an object in the server,
different clients are accessing the same object:
(The following example is modified from that given in the module
documentation: http://docs.python.org/lib/module-SimpleXMLRPCServer.html)


$ cat server.py
#!/usr/bin/env python

from SimpleXMLRPCServer import SimpleXMLRPCServer

# Create server
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)

# Register a function under a different name
def adder_function(x,y):
    return x + y
server.register_function(adder_function, 'add')


# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyObj:
    SEQ = None

    def __init__(self):
        self.SEQ = range(10000)
        self.index = 0

    def next_seq(self):
        n = self.SEQ[self.index]
        self.index += 1
        return n

server.register_instance(MyObj())

# Run the server's main loop
server.serve_forever()


# First client calls:
>>> import xmlrpclib
>>> url = 'http://localhost:8000'
>>> s = xmlrpclib.Server(url)
>>> s.next_seq()
0
>>> s.next_seq()
1
>>> s.next_seq()
2


# second client calls
>>> import xmlrpclib
>>> url = 'http://localhost:8000'
>>> s = xmlrpclib.Server(url)
>>> s.next_seq()
3


    I did not programmatically drive these calls at the same time to try to
force simultaneous access from separate requests, but my intuition is that
it's a pretty good bet that SimpleXMLRPCServer has taken care of re-entrant
issues.  It may be instructive for you to run that test yourself and
explicitly verify requests do not interact.

    Alternatively, if you really want to know for sure... Python is an
open-source project. You can dig around in SimpleXMLRPCServer.py yourself
and figure out exactly what it is doing:

>>> import SimpleXMLRPCServer
>>> SimpleXMLRPCServer
<module 'SimpleXMLRPCServer' from
'/usr/lib/python2.4/SimpleXMLRPCServer.pyc'>
>>>

ej at myBox ~
$ vi /usr/lib/python2.4/SimpleXMLRPCServer.py



see also:
http://docs.python.org/lib/module-BaseHTTPServer.html
http://docs.python.org/lib/module-SocketServer.html







More information about the Python-list mailing list