SimpleXMLRPCServer and creating a new object on for each new clientrequest.

J

Jelle Smet

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:
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
 
P

Piet van Oostrum

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

You create a single instance of your class and then register that. There
is nothing in your code that makes the server believe it should create a
new instance for each request.

And then of course this instance always returns the same random number.
It could generate a new random number for each call as follows:
def show_random(self):
return random.randrange(0,100000)
but I guess this is not what you want.

Otherwise you would have to register a function that creates a new
instance on every call.
But as Martin P. Hellwig has noted, it wouldn't give you sessions anyway.
 
P

Piet van Oostrum

One more thing:
JS> I start python interactively:

I get the impression, also from your use of the variable names
'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some
kind of connection to the XMLRPC server. This is not the case. It gives
just an administration object *in the client* that will communicate with
the server when you call a method on it. The two session's in your code
are therefore functionally equivalent and there is no advantage in
having two of them instead of one. And the name session is misleading.

Please note also that XMLRPC isn't object-oriented. There is just the
server; in the protocol there are no objects other than the server.
 
P

Piet van Oostrum

Piet van Oostrum said:
PvO> One more thing:
JS> I start python interactively:
PvO> I get the impression, also from your use of the variable names
PvO> 'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some
PvO> kind of connection to the XMLRPC server.

This should have been: I get the impression, also from your use of the
variable names 'session1' and 'session2', *that you think* that
xmlrpclib.ServerProxy() gives you some kind of connection to the XMLRPC
server.
 
G

google

One more thing:


I get the impression, also from your use of the variable names
'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some
kind of connection to the XMLRPC server. This is not the case. It gives
just an administration object *in the client* that will communicate with
the server when you call a method on it. The two session's in your code
are therefore functionally equivalent and there is no advantage in
having two of them instead of one. And the name session is misleading.

Please note also that XMLRPC isn't object-oriented. There is just the
server; in the protocol there are no objects other than the server.

Hi Piet,
Yes, I'm aware of this.
You create a single instance of your class and then register that. There
is nothing in your code that makes the server believe it should create a
new instance for each request.
On the other hand, If SimpleXMLRPCServer had this capability, my example wouldn't be correct anyway, because only one instance of class Randomizer is >created when the SimpleXMLRPCServer starts.

Exactly, I understand.
Otherwise you would have to register a function that creates a new
instance on every call.
But as Martin P. Hellwig has noted, it wouldn't give you sessions anyway.

Well, I think Martin's example will suit my needs.

Thanks for the explanation!

Jelle

Thanks for the feedback, ...
 
P

Piet van Oostrum

g> Well, I think Martin's example will suit my needs.
g> Thanks for the explanation!

His client code is unnecessarily complicated with 3 session variables.
The following code does the same:

SESSION = xmlrpclib.ServerProxy(URL_PORT)
print(SESSION.show_random())
print(SESSION.show_random())
SESSION.shutdown_service()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top