Created
October 25, 2017 13:05
-
-
Save LeoCBS/2222e6e7bc75c5ecd24a5c2eebc456e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based from twisted doc | |
# https://twistedmatrix.com/documents/16.5.0/core/howto/defer.html | |
from twisted.internet import reactor, defer | |
def getDeferredObj(inputData): | |
print('getDeferredObj called') | |
deferred = defer.Deferred() | |
# simulate a delayed result by asking the reactor to fire the | |
# Deferred in 2 seconds time with the result inputData * 3 | |
reactor.callLater(2, deferred.callback, inputData * 3) | |
return deferred | |
def printDataCallback(result): | |
""" | |
this function is called asynchronous by twisted | |
""" | |
print('Result received: {}'.format(result)) | |
# stopping reactor manually | |
reactor.stop() | |
deferred = getDeferredObj(3) | |
deferred.addCallback(printDataCallback) | |
# start up the Twisted reactor (event loop handler) manually | |
print('Starting the reactor') | |
reactor.run() | |
# RESULT | |
# getDeferredObj called | |
# Starting the reactor | |
# Result received: 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment