Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is twisted.internet.reactor global?

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?

like image 343
lightweight Avatar asked Aug 06 '10 14:08

lightweight


People also ask

What is twisted Internet reactor?

The reactor is the Twisted event loop within Twisted, the loop which drives applications using Twisted. The reactor provides APIs for networking, threading, dispatching events, and more.

What is the use of twisted in Python?

Twisted is an open source asynchronous event-driven engine for network application development written entirely in Python and distributed under the MIT license. It allows you to create a SMTP, HTTP, proxy and ssh servers in a matter of minutes without the use of traditional threading models.

What is twisted framework?

Twisted is an open source network framework written entirely in Python. It allows you to create a SMTP, HTTP, proxy and ssh servers (and more) in Python with minimal effort.

What is twisted package?

Twisted is an event-based framework for internet applications, supporting Python 3.6+. It includes modules for many different purposes, including the following: twisted. web: HTTP clients and servers, HTML templating, and a WSGI server.


1 Answers

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.

like image 179
Alex Martelli Avatar answered Oct 01 '22 13:10

Alex Martelli