inlineCallbacks 函数内的回调

发布于 2024-11-14 08:16:59 字数 1381 浏览 3 评论 0原文

假设我有一个像这样的函数:

def display(this, that):
    print this, that

和一个类:

class Runner(object):
    def __init__(self, callback):
        self.callback = callback
        self.loop = twisted.internet.task.LoopingCall(repeat)
        self.loop.start(0)

    @defer.inlineCallbacks
    def repeat(self):
        this = yield do_this()
        that = yield do_that()

        if this and that:
            # now I want to call the callback function
            yield self.callback(this, that) # makes sense?

runner = Runner(display)
reactor.run()

基本上我想做的是创建一个 Runner 类,它将执行一些特定的任务,并且每次获得结果时,它都会调用给定的回调函数。我不想创建一个执行特定操作的新函数,而是创建一个只执行一件事的通用类。例如:

class TwitterReader(object):
    def __init__(self, callback):
        ...
        ...

    @defer.inlineCallbacks
    def get_messages(self):
        ...
        ...
        yield callback(messages)

class MessageFilter(object):
    def __init__(self):
        self.bad_messages = open('bad_messages.txt', 'w')
        self.twitter = TwitterReader(self.message_received)

    def message_received(messages):
        for message in messages:
            for bad_word in BAD_WORDS:
                if bad_word in message:
                    self.bad_messages.write(message)
                    break

我是扭曲的新手。所以,我不确定这是否是正确的方法。是吗?

谢谢

Let's say I have a function like this:

def display(this, that):
    print this, that

and a class:

class Runner(object):
    def __init__(self, callback):
        self.callback = callback
        self.loop = twisted.internet.task.LoopingCall(repeat)
        self.loop.start(0)

    @defer.inlineCallbacks
    def repeat(self):
        this = yield do_this()
        that = yield do_that()

        if this and that:
            # now I want to call the callback function
            yield self.callback(this, that) # makes sense?

runner = Runner(display)
reactor.run()

Basically what I want to do is I want to create a Runner class which will do some specific tasks and every time it gets a result, it will call the given callback function. Instead of creating a new function which does a specific thing, I want to create a generic class which does only one thing. E.g:

class TwitterReader(object):
    def __init__(self, callback):
        ...
        ...

    @defer.inlineCallbacks
    def get_messages(self):
        ...
        ...
        yield callback(messages)

class MessageFilter(object):
    def __init__(self):
        self.bad_messages = open('bad_messages.txt', 'w')
        self.twitter = TwitterReader(self.message_received)

    def message_received(messages):
        for message in messages:
            for bad_word in BAD_WORDS:
                if bad_word in message:
                    self.bad_messages.write(message)
                    break

I'm new to twisted. So, I'm not sure if this is the right way to do it. Is it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-11-21 08:16:59

您的问题是 repeat 中的 callback 应该改为 self.callback

除此之外,您的示例应该完全按照编写的方式工作。

Your problem is that callback inside repeat should instead be self.callback.

Other than that your example should work exactly as written.

夜吻♂芭芘 2024-11-21 08:16:59

如果它返回延迟并且您希望在退出 repeat 函数之前等待结果,则您只需需要yield self.callback。在您的示例中,您的回调是一个普通函数(它实际上返回 None),因此生成没有任何优势 - 但是它允许生成非延迟值,因此不会造成任何损害。来自 inlineCallbacks 文档

非 Deferreds 的东西也可能被产生,并且你的生成器
将恢复并发送回相同的对象。这意味着产量
执行大致相当于 MaybeDeferred 的操作。

如果您的回调did返回一个延迟函数(例如,如果它也是一个inlineCallbacks修饰函数),那么yielding将暂停repeat的执行,直到延迟函数完全的。这在您的应用程序中可能是理想的,也可能不是。

You'd only need to yield self.callback if it returned a deferred and you wanted to wait for the result before exiting the repeat function. In your example, your callback is a normal function (which effectively returns None), so there is no advantage to yielding - however it is allowed to yield non-deferred values so no harm is done. From the inlineCallbacks docs:

Things that are not Deferreds may also be yielded, and your generator
will be resumed with the same object sent back. This means yield
performs an operation roughly equivalent to maybeDeferred.

If your callback did return a deferred (eg, if it was also an inlineCallbacks decorated function) then yielding would pause execution of repeat until the deferred completed. This may or may not be desirable in your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文