inlineCallbacks 函数内的回调
假设我有一个像这样的函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是
repeat
中的callback
应该改为self.callback
。除此之外,您的示例应该完全按照编写的方式工作。
Your problem is that
callback
insiderepeat
should instead beself.callback
.Other than that your example should work exactly as written.
如果它返回延迟并且您希望在退出
repeat
函数之前等待结果,则您只需需要yield self.callback
。在您的示例中,您的回调是一个普通函数(它实际上返回None
),因此生成没有任何优势 - 但是它允许生成非延迟值,因此不会造成任何损害。来自 inlineCallbacks 文档:如果您的回调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 therepeat
function. In your example, your callback is a normal function (which effectively returnsNone
), 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:If your callback did return a deferred (eg, if it was also an
inlineCallbacks
decorated function) then yielding would pause execution ofrepeat
until the deferred completed. This may or may not be desirable in your application.