首页 > 其他 > 详细

理解twisted中的reactor和deferred

时间:2019-11-25 13:35:07      阅读:89      评论:0      收藏:0      [点我收藏+]
from twisted.internet import reactor, defer

def getDummyData(inputData):
    """
    This function is a dummy which simulates a delayed result and
    returns a Deferred which will fire with that result. Don‘t try too
    hard to understand this.
    """
    print(‘getDummyData 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 cbPrintData(result):
    """
    Data handling function to be added as a callback: handles the
    data by printing the result
    """
    print(‘Result received: {}‘.format(result))

deferred = getDummyData(3)
deferred.addCallback(cbPrintData)

# manually set up the end of the process by asking the reactor to
# stop itself in 4 seconds time
reactor.callLater(4, reactor.stop)
# start up the Twisted reactor (event loop handler) manually
print(‘Starting the reactor‘)
reactor.run()

  

理解twisted中的reactor和deferred

原文:https://www.cnblogs.com/WalkOnMars/p/11927096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!