Twisted:延期,反复触发? [英] Twisted: deferred that fires repeatedly?

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

问题描述

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.

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

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