扭曲:延迟重复触发? [英] Twisted: deferred that fires repeatedly?

查看:154
本文介绍了扭曲:延迟重复触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Deferred 是在Twisted中进行异步处理的好方法。然而,像名字所暗示的,它们是用于延迟计算,它只运行和终止一次,触发一次回调。如果我有一个重复的计算,如按钮被点击怎么办?是否有任何 Deferred 一样的对象,可以反复触发,调用附加到它的所有回调,当它被触发时?

Deferreds are a great way to do asynchronous processing in Twisted. However, they, like the name implies, are for deferred computations, which only run and terminate once, firing the callbacks once. What if I have a repeated computation, like a button being clicked? Is there any Deferred-like object that can fire repeatedly, calling all callbacks attached to it whenever it is fired?

推荐答案

我已经设置好了。对我的有限用例它做我想要的。

I've set this up for now. For my limited use case it does what I want.

class RepeatedDeferred:
    def __init__(self):
        self.callbacks = []

        self.df = defer.Deferred()

    def addCallback(self, callback):
        self.callbacks.append(callback)

        self.df.addCallback(callback)

    def callback(self, res):
        self.df.callback(res)

        self.df = defer.Deferred()
        for c in self.callbacks:
            self.df.addCallback(c)

有人让我知道这是否可怕。

Someone let me know if this is terrible.

这篇关于扭曲:延迟重复触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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