python - LoopingCall AMP commands -


why error while trying implement loopingcall function calling amp commands?

from twisted.protocols.amp import amp twisted.python.log import startlogging, err twisted.internet.task import loopingcall twisted.internet import reactor sys import stdout  import commands  startlogging(stdout)   class myamp:      def __init__(self, host, port):         destination = tcp4clientendpoint(reactor, host, port)         self.protocol = amp()         self.d = connectprotocol(destination, self.protocol)      def say(self):         return self.protocol.callremote(commands.say,                                    phrase='hello world')   def loop(myamp):     myamp.say()   def main(host, port):     myamp = myamp(host, port)     lc = loopingcall(loop, myamp=myamp)     lc.start(4.0)     reactor.run()  main('127.0.0.1', 12345) 

error while calling myamp.say() within loop:

2013-08-16 12:28:58-0400 [-] starting factory <twisted.internet.endpoints.oneshotfactory instance @ 0x92273ec> 2013-08-16 12:28:58-0400 [-] unhandled error in deferred: 2013-08-16 12:28:58-0400 [-] unhandled error     traceback (most recent call last):       file "lib/client.py", line 35, in <module>         main('127.0.0.1', 12345)       file "lib/client.py", line 32, in main         lc.start(4.0)       file "/usr/local/lib/python2.7/site-packages/twisted/internet/task.py", line 173, in start         self()       file "/usr/local/lib/python2.7/site-packages/twisted/internet/task.py", line 218, in __call__         d = defer.maybedeferred(self.f, *self.a, **self.kw)     --- <exception caught here> ---       file "/usr/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 137, in maybedeferred         result = f(*args, **kw)       file "lib/client.py", line 26, in loop         myamp.say()       file "lib/client.py", line 22, in         phrase='hello world')       file "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 821, in callremote         return co._docommand(self)       file "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1778, in _docommand         self.requiresanswer)       file "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 752, in _sendboxcommand         box._sendto(self.boxsender)       file "/usr/local/lib/python2.7/site-packages/twisted/protocols/amp.py", line 577, in _sendto         proto.sendbox(self)     exceptions.attributeerror: 'nonetype' object has no attribute 'sendbox'  2013-08-16 12:28:58-0400 [uninitialized] amp connection established (host:ipv4address(tcp, '127.0.0.1', 50457) peer:ipv4address(tcp, '127.0.0.1', 12345)) 

you're trying callremote before connection established. loopingcall will, default, run function when start it. instead of doing lc.start(4.0), lc.start(4.0, now=false). wait 4 second before first call.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -