[Twisted-Python] xmlrpc - Possable failure to understand async concept
Tim Hughes
thughes at thegoldfish.org
Thu Apr 23 20:26:04 EDT 2009
I am trying to understand this concept of async methods and i thought I had
it cracked with the code below but it doesn't seem to work as all the calls
to the xmlrpc server return one after the other like this:
Success:I started at Fri Apr 24 01:12:23 2009 and i slept 5 seconds then
woke at Fri Apr 24 01:12:28 2009
Success:I started at Fri Apr 24 01:12:28 2009 and i slept 5 seconds then
woke at Fri Apr 24 01:12:33 2009
Success:I started at Fri Apr 24 01:12:33 2009 and i slept 5 seconds then
woke at Fri Apr 24 01:12:38 2009
Success:I started at Fri Apr 24 01:12:38 2009 and i slept 5 seconds then
woke at Fri Apr 24 01:12:43 2009
Success:I started at Fri Apr 24 01:12:43 2009 and i slept 5 seconds then
woke at Fri Apr 24 01:12:48 2009
Success:I started at Fri Apr 24 01:12:48 2009 and i slept 5 seconds then
woke at Fri Apr 24 01:12:53 2009
Am I completly missing the point here or is there something incorrect with
my code. AT first I thought it was because the sleep was shutting the whole
thing down (probably is) so I have also tried it with a long running
network call instead of the time.sleep() and have the same issue. Can
someone please point me in the right direction or give a bit of criticism of
the code concepts/thought processes
=========== Server ========================
#!/usr/bin/env python
from twisted.web import xmlrpc, server
from twisted.internet.defer import Deferred
from twisted.internet import reactor, threads
import time
class MyServer(xmlrpc.XMLRPC):
def blocking_method(self, duration=5):
"""block the instance for a specified duration"""
started = time.asctime()
time.sleep(duration)
data = "I started at %s and i slept %d seconds then woke at %s" %
(started, duration, time.asctime())
return data
# def xmlrpc_block_thread(self, duration=10):
# """ pass to blocking method using thread """
# return threads.deferToThread(self.blocking_method, duration)
#
# def xmlrpc_block(self, duration=10):
# """ pass to blocking method using defered """
# d = Deferred()
# d.callback(self.blocking_method(duration))
# return d
def xmlrpc_block_fixed(self, duration=5):
""" pass to blocking method using defered """
d = self.blocking_method_fixed(duration) # return a defered
d.addCallback(self.printdata)
d.addErrback(self.printerror)
return d
def blocking_method_fixed(self, duration=5):
d = Deferred()
d.callback(self.blocking_method(duration))
return d
def printdata(self, val):
return 'Success:'+val
def printerror(self, val):
return 'Error:'+val
if __name__ == '__main__':
r = MyServer()
reactor.listenTCP(8080, server.Site(r))
reactor.run()
===============================================
========== Client ================================
#!/usr/bin/env python
import xmlrpclib
import datetime
import sys
proxy = xmlrpclib.ServerProxy("http://localhost:8080/", allow_none=True)
print "%s" % str(proxy.block_fixed(5))
===============================================
Tim Hughes
mailto:thughes at thegoldfish.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090424/abdb0561/attachment.htm
More information about the Twisted-Python
mailing list