SimpleXMLRPCServer and creating a new object on for each new client request.

J python-list at smetj.net
Wed May 6 17:27:51 EDT 2009


Hi list,

My goals is to have concurrent and separated client sessions using xmlrpc.
Initially my though was that SimpleXMLRPCServer was able to create a new
object instance for each incoming request.
But this doesn't appear to be the case, unless I'm overlooking something,
if so please point me out.

Concider following simplified code


#!/usr/bin/python

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import random

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create a simple example class
class Randomizer:
        def __init__(self):
                self.random=random.randrange(0,100000)
        def show_random(self):
                return self.random

# Create server
server = SimpleXMLRPCServer(("localhost",
8000),requestHandler=RequestHandler,allow_none=1)
server.register_introspection_functions()

server.register_instance(Randomizer())
server.serve_forever()



I start python interactively:
>>> import xmlrpclib
>>> session1=xmlrpclib.ServerProxy('http://localhost:8000')
>>> session2=xmlrpclib.ServerProxy('http://localhost:8000')
>>> print session1.show_random()
13930
>>> print session2.show_random()
13930
>>> 

I though that session1 and session2 would be 2 different Randomizer objects
each having a different result for self.random
But as the example shows this is not the case.
How can I solve this?

Thanks

Jelle




More information about the Python-list mailing list