delavnog delavnog - 1 year ago 145
Python Question

How to combine callLater and addCallback?

This is so broken, I hope you are merciful with me:

reactor.callLater(0, myFunction, parameter1).addCallback(reactor.stop)

returns a deferred.

I hope it is clear what I want to do:

  • as soon as the reactor is running, I want to call
    . That is why I am using 0 as the delay parameter. Is there no other way except callLater? It looks funny to pass it a delay of 0.

  • I want to stop the reactor as soon as
    has completed the task.

The problems that I have so far:

  • AttributeError: DelayedCall instance has no attribute 'addCallback'
    . Fair enough! How do I put a callback in the callback chain started by

  • exceptions.TypeError: stop() takes exactly 1 argument (2 given)

To solve the second problem I had to define a special function:

def stopReactor(result): 'Result: %s' % result) 'Stopping reactor immediatelly' )

And change the code to:

reactor.callLater(0, myFunction, parameter1).addCallback(stopReactor)

(still not working because of the callLater problem, but
will work now)

Is there really no other way to call
except by defining an extra function?

Answer Source

IReactorTime.callLater and Deferred are mixed together by twisted.internet.task.deferLater.

from twisted.internet import reactor, task

d = task.deferLater(reactor, 0, myFunction, parameter1)
d.addCallback(lambda _: reactor.stop())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download