Twisted.internet.reactor 是全球性的吗? [英] Is twisted.internet.reactor global?

查看:64
本文介绍了Twisted.internet.reactor 是全球性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果一个应用程序执行fromtwisted.internet import reactor,而另一个应用程序执行相同操作,那么这些reactors 是否相同?

For example, if one application does from twisted.internet import reactor, and another application does the same, are those reactors the same?

我之所以这么问是因为 Deluge 是一个使用 Twisted 的应用程序,看起来它使用反应器来连接他们的UI (gtk) 到由扭曲驱动的应用程序的其余部分(我试图了解源代码).例如,当 UI 关闭时,它只需调用 reactor.stop().

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?

推荐答案

是的,Python 中的每个模块始终是全局的,或者更确切地说,是一个单例:当你执行 fromtwisted.internet import reactor,Python 的导入机制首先检查 sys.modules['twisted.internet.reactor'],如果存在,则返回该值;只有当它不存在(即第一次导入模块时)才是第一次实际加载的模块(并隐藏在 sys.modules 中的一个条目中以备将来导入).

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 checks sys.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 in sys.modules for possible future imports).

Singleton 设计模式没有什么特别神奇的地方,尽管当您迫切需要多个架构规定只能只有一个"的东西时,它有时会受到限制.Twisted 的 docs 承认:

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:

新的应用程序代码应该更喜欢通过并接受反应堆作为需要的参数,而不是而不是依靠能够进口这个模块得到一个参考.这简化单元测试,并可能使一天支持多个更容易反应器(作为性能增强),虽然这不是目前可能.

New application code should prefer to pass and accept the reactor as a parameter where it is needed, rather than relying on being able to import this module to get a reference. This simplifies unit testing and may make it easier to one day support multiple reactors (as a performance enhancement), though this is not currently possible.

如果它对您的应用程序至关重要,则使其成为可能的最佳方法是为 Twisted 项目做出贡献,无论是劳动(编码支持多个反应器所需的微妙机制,即多个事件循环,在单个应用程序中) 或资金(金钱可以维持某人的津贴以完成这项工作).

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).

否则,使用单独的进程(例如,使用标准库的 multiprocessing 模块),每个进程不超过一个反应器.

Otherwise, use separate processes (e.g. with the multiprocessing module of the standard library) with no more than one reactor each.

这篇关于Twisted.internet.reactor 是全球性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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