码迷,mamicode.com
首页 > 其他好文 > 详细

理解twisted中的reactor和deferred(二)

时间:2019-11-25 15:12:57      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:ref   code   matrix   second   ase   first   val   bsp   cti   

Deferred可以添加多个回调函数,每个回调函数的结果作为下一个回调函数的参数

 

代码实例(可在pycharm中运行,摘自 https://twistedmatrix.com/documents/current/core/howto/defer.html

from twisted.internet import reactor, defer

class Getter:
    def gotResults(self, x):
        """
        The Deferred mechanism provides a mechanism to signal error
        conditions.  In this case, odd numbers are bad.

        This function demonstrates a more complex way of starting
        the callback chain by checking for expected results and
        choosing whether to fire the callback or errback chain
        """
        if self.d is None:
            print("Nowhere to put results")
            return

        d = self.d
        self.d = None
        if x % 2 == 0:
            d.callback(x*3)  # 执行d的第一个回调函数,也就是_toHTML函数
        else:
            d.errback(ValueError("You used an odd number!")) # 执行d的errorback回调函数,也就是ebPrintError

    def _toHTML(self, r):
        """
        This function converts r to HTML.

        It is added to the callback chain by getDummyData in
        order to demonstrate how a callback passes its own result
        to the next callback
        """
        return "Result: %s" % r  #返回给第二个回调cbPrintData函数做参数

    def getDummyData(self, x):
        """
        The Deferred mechanism allows for chained callbacks.
        In this example, the output of gotResults is first
        passed through _toHTML on its way to printData.

        Again this function is a dummy, simulating a delayed result
        using callLater, rather than using a real asynchronous
        setup.
        """
        self.d = defer.Deferred()
        # simulate a delayed result by asking the reactor to schedule
        # gotResults in 2 seconds time
        reactor.callLater(2, self.gotResults, x)  # 2秒后执行gotResults函数
        self.d.addCallback(self._toHTML)  # 添加第一个回调函数
        return self.d

def cbPrintData(result):
    print(result)

def ebPrintError(failure):
    import sys
    sys.stderr.write(str(failure))

# this series of callbacks and errbacks will print an error message
g = Getter()
d = g.getDummyData(3)
d.addCallback(cbPrintData)
d.addErrback(ebPrintError)

# this series of callbacks and errbacks will print "Result: 12"
g = Getter()
d = g.getDummyData(4)
d.addCallback(cbPrintData) # 添加第二个回调函数
d.addErrback(ebPrintError)

reactor.callLater(4, reactor.stop)
reactor.run()

 

结果:

技术图片

 

 

 

理解twisted中的reactor和deferred(二)

标签:ref   code   matrix   second   ase   first   val   bsp   cti   

原文地址:https://www.cnblogs.com/WalkOnMars/p/11927687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!