Welcome, guest | Sign In | My Account | Store | Cart

Make the standard-lib SimpleXMLRPCServer multi-threaded by using a simple mix-in construction.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Guyon Morée
# http://gumuz.looze.net/

import SocketServer
from SimpleXMLRPCServer import SimpleXMLRPCServer,SimpleXMLRPCRequestHandler
 
# Threaded mix-in
class AsyncXMLRPCServer(SocketServer.ThreadingMixIn,SimpleXMLRPCServer): pass
 
# Example class to be published
class TestObject:
    def pow(self, x, y):
        return pow(x, y)
 
    def add(self, x, y) :
        return x + y
 
    def divide(self, x, y):
        return float(x) / float(y)
 
 
# Instantiate and bind to localhost:8080
server = AsyncXMLRPCServer(('', 8080), SimpleXMLRPCRequestHandler)
 
# Register example object instance
server.register_instance(TestObject())
 
# run!
server.serve_forever()

This implementation will probably have some scalability issues and it can't compete with the feature-set which for example Twisted has.

But I like the fact it's totally build out of standard library python modules and great for prototyping XML-RPC ideas.

As presented here: http://gumuz.looze.net/wordpress/index.php/archives/2004/12/13/async-xml-rpc-server/

4 comments

Philippe 14 years, 9 months ago  # | flag

Not really async per se, but rather threaded... Howver a superb example of python mixin power

Larnim'A 14 years, 1 month ago  # | flag

Hey,

I really think this way of using python is cool but, I don't think I understand everything ... (I'm too newbie in python :P)

and i have a question : is there a way to create a multi-thread rpc server (like you did) and keep a reference on this thread, then start the server without "blockling" my script. i.e. : some where in my code I have something like that :

self._LocalServer = _SimpleThreadedXMLRPCServer(("localhost",port))
self._LocalServer.register_instance(_DCSvrFunctions())
self._LocalServer.serve_forever()

the trouble is serve_forever never return ... is there a way to make my code work ? I just want to create a server, keep a reference on it to call some method from time to time and I want to make it serve forever (without freezing my main script :P)

thanks in advance ... I know that should sound stupid and basic for you guys but not for a newbie in python :P

Larnim'A 14 years, 1 month ago  # | flag

sorry I forget to point out what's wrong with the previews code ...

here is my trouble :

self._LocalServer = _SimpleThreadedXMLRPCServer(("localhost",port))
self._LocalServer.register_instance(_DCSvrFunctions())
self._LocalServer.serve_forever()
print "this will never be printed ..."

and I just want to make my Localserver run in its own thread and let my script go one and print this string :)

another 13 years, 11 months ago  # | flag

Larnim'A, I did what you are asking for. send me an email and we can discuss that. anothertrad@gmail.com