Question about xmlrpc and threading
Martin P. Hellwig
mhellwig at xs4all.nl
Wed Feb 15 18:50:49 EST 2006
David Hirschfield wrote:
> An xmlrpc client/server app I'm writing used to be super-simple, but now
> threading has gotten into the mix.
>
> On the server side, threads are used to process requests from a queue as
> they come in.
> On the client side, threads are used to wait on the results of requests
> to the server.
>
> So the question is: how thread-safe is python xmlrpc? If the client
> makes a request of the server by calling:
>
> result = server.doSomething()
>
> and while that is waiting in the background thread to complete, the
> client calls another:
>
> result = server.doSomethingElse()
>
> will they interfere with each other? Right now I'm avoiding this problem
> by queueing up calls to the server to be processed sequentially in the
> background. But I'd prefer to allow requests to go in parallel. Should I
> just make a new connection to the server for each request?
>
> Any advice appreciated,
> -David
>
I'm not sure if I quite understand what you mean by "interfere with each
other" but namespaces also apply to xmlrpc servers.
But let's give a coding example say I have this async server created:
===
from SocketServer import ThreadingMixIn
from SimpleXMLRPCServer import SimpleXMLRPCServer
from time import sleep
# Overriding with ThreadingMixIn to create a async server
class txrServer(ThreadingMixIn,SimpleXMLRPCServer): pass
# the Test classs
class Test(object):
def __init__(self):
self.returnValue = "Something 1"
def doSomething(self):
sleep(5)
return self.returnValue
def doSomethingElse(self,value):
self.returnValue=value
return self.returnValue
# setup server and bind to the specified port
server = txrServer(('localhost', 8080))
# register test class
server.register_instance(Test())
# start the serving
server.serve_forever()
===
And I call the function
>>> doSomething()
and while it's waiting I call
>>> doSomethingElse("Else What!"),
doSomething() will return me:"Else What!" instead of "Something 1"
because it's a shared namespace of "self".
Now if I modify my example to this:
===
from SocketServer import ThreadingMixIn
from SimpleXMLRPCServer import SimpleXMLRPCServer
from time import sleep
# Overriding with ThreadingMixIn to create a async server
class txrServer(ThreadingMixIn,SimpleXMLRPCServer): pass
# the Test classs
class Test(object):
def __init__(self):
pass
def doSomething(self):
returnValue = "Something 1"
sleep(5)
return returnValue
def doSomethingElse(self,value):
returnValue=value
return returnValue
# setup server and bind to the specified port
server = txrServer(('localhost', 8080))
# register test class
server.register_instance(Test())
# start the serving
server.serve_forever()
===
>>> doSomethingElse("Now what?")
Will have no effect on returnValue of doSomething() because they are not
shared.
But say that I add the sleep part to doSomethingElse() and call
>>> doSomethingElse("First")
and immediately after that on a other window
>>> doSomethingElse("other")
What do you think will happen? Will the 2nd call overwrite the firsts
calls variable?
I'm not going to spoil it any further ;-), please try the snippets out
for yourself (I bet you'll be pleasantly surprised).
hth
--
mph
More information about the Python-list
mailing list