twisted.internet.reactor 是全球性的吗?
例如,如果一个应用程序执行 fromtwisted.internet importreactor
,而另一个应用程序执行相同的操作,那么这些 reactor
是否相同?
我问这个问题是因为 Deluge 是一个使用 Twisted 的应用程序,看起来它使用 Reactor 来连接其 UI(gtk )到由扭曲驱动的应用程序的其余部分(我正在尝试了解来源)。例如,当 UI 关闭时,它只需调用 reactor.stop()
。
这就是全部内容了吗?对我来说这似乎有点神奇。如果我想运行另一个使用twisted 的应用程序怎么办?
For example, if one application does from twisted.internet import reactor
, and another application does the same, are those reactors
the same?
I am asking because Deluge, an application that uses twisted, looks like it uses the reactor to connect their UI (gtk) to the rest of the application being driven by twisted (I am trying to understand the source). For example, when the UI is closed it simply calls reactor.stop()
.
Is that all there is to it? It just seems kind of magic to me. What if I wanted to run another application that uses twisted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,Python 中的每个模块始终是全局的,或者,更好地说,是单例:当您执行
fromtwisted.internet importreactor
时,Python 的导入机制首先检查sys.modules[' twisted.internet.reactor']
,如果存在,则返回所述值;仅当它不存在时(即,第一次导入模块时),该模块才是第一次实际加载(并隐藏到 sys.modules 中的条目中以供将来可能导入)。单例设计模式并没有什么特别神奇的地方,尽管当你迫切需要超过一件架构规定“只能有一个”的东西时,它有时会被证明是有限制的。 Twisted 的文档承认:
如果它对您的应用程序至关重要,那么实现这一目标的最佳方法就是为 Twisted 项目做出贡献,无论是劳动力(编码支持多个反应器所需的微妙机制,即单个应用程序中的多个事件循环)还是提供资金(金钱可以维持某人的津贴以完成这项工作)。
否则,请使用单独的进程(例如,使用标准库的
multiprocessing
模块),每个进程不超过一个反应器。Yes, every module in Python is always global, or, to put it better, a singleton: when you do
from twisted.internet import reactor
, Python's import mechanism first checkssys.modules['twisted.internet.reactor']
, and, if that exists, returns said value; only if it doesn't exist (i.e., the first time a module is imported) is the module actually loaded for the first time (and stashed into an entry insys.modules
for possible future imports).There is nothing especially magical in the Singleton design pattern, though it can sometimes prove limiting when you desperately need more than one of those thingies for which the architecture has decreed "there can be only one". Twisted's docs acknowledge that:
The best way to make it possible, if it's crucial to your app, is to contribute to the Twisted project, either labor (coding the subtle mechanisms needed to support multiple reactors, that is, multiple event loops, within a single app) or funding (money will enable sustaining somebody with a stipend in order to perform this work).
Otherwise, use separate processes (e.g. with the
multiprocessing
module of the standard library) with no more than one reactor each.该反应堆确实是全球性的。它负责事件循环,并且您可以注册处理程序来使用事件。如果你想使用同一个反应器的多个应用程序,你可以使用twistd守护进程。 http://twistedmatrix.com/documents/current/core/howto/application。 html
The reactor is indeed global. It takes care of the event loop, and you register handlers to consume events. If you want to use several applications with the same reactor, you can use the twistd daemon. http://twistedmatrix.com/documents/current/core/howto/application.html