在条件下停止扭曲反应器 [英] Stop twisted reactor on a condition

查看:34
本文介绍了在条件下停止扭曲反应器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在达到一定条件时停止扭曲的反应器.例如,如果一个变量被设置为某个值,那么反应器应该停止吗?

Is there a way to stop the twisted reactor when a certain condition is reached. For example, if a variable is set to certain value, then the reactor should stop?

推荐答案

理想情况下,您不会将变量设置为值并停止反应器,而是调用 reactor.stop().有时您不在主线程中,这是不允许的,因此您可能需要调用 reactor.callFromThread.以下是三个工作示例:

Ideally, you wouldn't set the variable to a value and stop the reactor, you'd call reactor.stop(). Sometimes you're not in the main thread, and this isn't allowed, so you might need to call reactor.callFromThread. Here are three working examples:

# in the main thread:
reactor.stop()

# in a non-main thread:
reactor.callFromThread(reactor.stop)

# A looping call that will stop the reactor on a variable being set, 
# checking every 60 seconds.
from twisted.internet import task
def check_stop_flag():
    if some_flag:
        reactor.stop()
lc = task.LoopingCall(check_stop_flag)
lc.start(60)

这篇关于在条件下停止扭曲反应器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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