Is twisted.internet.reactor global?

萝らか妹 提交于 2020-01-14 07:56:07

问题


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?


回答1:


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

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.

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.




回答2:


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



来源:https://stackoverflow.com/questions/3424825/is-twisted-internet-reactor-global

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!