Go to the latest version of this document.

# ``twisted`.`internet`.`reactor``

module documentation
(source)

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.

This module is the way to access the current global reactor, by doing:

        from twisted.internet import reactor

The specific capabilities of the default reactor depends on your platform, and will be installed if this module is imported without another reactor being explicitly installed beforehand. Regardless of which reactor is installed, importing this module is the way to get a reference to it.

In order to minimize dependencies on mutable global state, 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.

To get the reactor at the beginning of your program without using this global import, use `twisted.internet.task.react`, like so:

        # If you need a custom reactor, you can install it at the beginning:
        from twisted.internet.custom_reactor import install
        install()

        from twisted.internet.task import react
        from twisted.internet.defer import Deferred

        async def main(reactor: IReactorTCP) -> None:
            reactor.listenTCP(...)
            await Deferred()        # wait forever

        if __name__ == '__main__':
            react(main, ())

This simplifies testing and may make it easier to one day support multiple reactors.

See Also
`IReactorCore`
`IReactorTime`
`IReactorProcess`
`IReactorTCP`
`IReactorSSL`
`IReactorUDP`
`IReactorMulticast`
`IReactorUNIX`
`IReactorUNIXDatagram`
`IReactorFDSet`
`IReactorSocket`
`IReactorWin32Events`
`IReactorThreads`
`IReactorPluggableResolver`
`IReactorDaemonize`

Note

Another reason to prefer getting the reactor as a parameter is that type information about the object you get from `from twisted.internet import reactor` is always slightly inaccurate. At runtime, interfaces are only contextually provided depending upon the installed reactor, the installed libraries, and the capabilities of your platform, so there is no "correct" type for this object to statically be. Thus, it provides a conglomeration of many of the most common reactor interfaces which are usually available.

To be fully correct in new code, it's best to always adapt the reactor to your desired interface in order to interrogate its capabilities; for example, if you need to call a method on `IReactorSSL` but provide useful error messages if it's not available, you can do something like this:

        from twisted.internet import reactor

        if (sslReactor := IReactorSSL(reactor, None)) is not None:
            sslReactor.listenSSL(...)
        else:
            print("TLS support not available.")