扭曲的延迟/回调和异步执行 [英] twisted deferred/callbacks and asynchronous execution

查看:105
本文介绍了扭曲的延迟/回调和异步执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用扭曲使代码更具异步性.

I'm trying to figure out how can i make my code more asynchronous using twisted.

  • 函数返回延迟的对象
  • 然后我添加一个回调列表
  • 在延迟函数通过deferred_obj.callback
  • 提供某些结果之后,将调用第一个回调
  • 然后,在回调链中,第一个回调将对数据进行处理,然后调用第二个回调
  • 等等
  • A function returns a deferred object
  • then i add a list of callbacks
  • the first callback will be called after the deferred function provides some result through deferred_obj.callback
  • then, in the chain of callbacks, the first callback will do something with the data and call the second callback
  • and etc.

但是链接的回调不会被视为异步的,因为它们被链接了,并且事件循环将不断触发每个回调,直到不再触发,对吧?

however chained callbacks will not be considered asynchronous because they're chained and the event loop will keep firing each one of them concurrently until there is no more, right?

但是,如果我有一个deferred对象,并且像d.addCallback(deferred_obj.callback)一样附加deferred_obj.callback作为其回调,则这将被视为异步,因为deferred_obj正在等待数据,然后将通过该方法数据也在等待数据,但是一旦d.callback'd'对象处理了数据,便调用deferred_obj.callback,但是由于此对象是延迟的,与链式回调不同,它将异步执行...正确?

However, if I have a deferred object, and I attach as its callback the deferred_obj.callback as in d.addCallback(deferred_obj.callback) then this will be considered asynchronous, because the deferred_obj is waiting for the data, and then the method that will pass the data is waiting on data as well, however once i d.callback 'd' object processes the data then it call deferred_obj.callback however since this object is deferred, unlike the case of chained callbacks, it will execute asynchronously... correct?

假设我所有的代码都是非阻塞的,这意味着链接的回调不是异步的,而链接的延迟是正确的吗?

Assuming all of my code is non-blocking, this means that chained callbacks are NOT asynchronous while chained deferreds are, correct?

推荐答案

(默认情况下)回调是同步的.但是,扭曲的文档指出:

The callbacks are (by default) synchronous. However, as the Twisted doc points out:

如果您需要一个Deferred来等待另一个,那么您要做的就是从添加到addCallbacks的方法中返回Deferred.

If you need one Deferred to wait on another, all you need to do is return a Deferred from a method added to addCallbacks.

因此,您可以使用它在回调链中执行一些异步处理.让我们做到这一点:

So you can use that to do some asynchronous processing in your callback chain. Let's do that:

from twisted.internet import reactor, defer

def callback_func_2(result, previous_data):
    # here we pass the result of the deferred down the callback chain
    # (done synchronously)
    print "calling function 1 on result:%s with previous result:%s" % (result, previous_data)
    return result

def callback_func(result):
    #let's do some asynchronous stuff in this callback
    # simple trick here is to return a deferred from a callback 
    # instead of the result itself.
    # 
    # so we can do asynchronous stuff here, 
    # like firing something 1 second later and have 
    # another method processing the result
    print "calling function 1 on result:%s" % result
    d = defer.Deferred()
    reactor.callLater(1, d.callback, "second callback")
    d.addCallback(callback_func_2, result)
    return d

def do():
    d = defer.Deferred()
    reactor.callLater(1, d.callback, "first callback")
    d.addCallback(callback_func)
    return d

do()
reactor.run()

这篇关于扭曲的延迟/回调和异步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆