1234567891011121314151617181920212223242526272829303132333435363738 |
- #!/usr/bin/env python
- from twisted.internet import reactor, defer
- from pprint import pprint
- def getData(inputData):
- print('getData({0})'.format(inputData))
- deferred = defer.Deferred()
- # simulate a delayed result by asking reactor
- # to fire in 2 seconds with inputData * 3 as result
- reactor.callLater(2, deferred.callback, inputData * 3)
- return deferred
- def cbPrint(result):
- print("result: {0}".format(result))
- # doing eferred, or the return result + 1 works
- deferred = defer.Deferred()
- reactor.callLater(1, deferred.callback, result + 1)
- return deferred
- # return result + 1
- def cbPrint2(result):
- print("result:")
- pprint(result)
- return result + 2
- d = getData(3)
- # Ok, chaining callbacks is ... different.
- d.addCallback(cbPrint)
- # Before return result, this was printing None.
- d.addCallback(cbPrint2)
- d.addCallback(cbPrint)
- reactor.callLater(5, reactor.stop)
- print("starting the reactor")
- reactor.run()
|