#!/usr/bin/env python # https://twistedmatrix.com/documents/16.2.0/core/howto/defer.html 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 print("return result (x*3) in 2 sec...") print(deferred) reactor.callLater(2, deferred.callback, inputData * 3) print(deferred) reactor.callLater(3, deferred.callback, inputData * 3) print(deferred) return deferred def cbPrint(result): print("cbPrint({0})".format(result)) # doing eferred, or the return result + 1 works print("return result (x+1) in 1 sec...") deferred = defer.Deferred() reactor.callLater(1, deferred.callback, result + 1) return deferred # return result + 1 def cbPrint2(result): print("cbPrint2({0})".format(result)) print("return result (x+2) ...") # print("(this isn't a defer)") # it is now! :P # return a Deferred object already called back with the value of result return defer.succeed(result + 2) # 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) d.addCallback(print) print("Stop the reactor in 7 seconds...") reactor.callLater(7, reactor.stop) print("starting the reactor") reactor.run() print("Reactor has been safely shutdown. :P")