Go to the latest version of this document.

# ``twisted`.`internet`.`asyncioreactor`.`AsyncioSelectorReactor``

class documentation

`class AsyncioSelectorReactor(PosixReactorBase): (source)`

Constructor: `AsyncioSelectorReactor(eventloop)`

Implements interfaces: `twisted.internet.interfaces.IReactorFDSet`

View In Hierarchy

Reactor running on top of `asyncio.SelectorEventLoop`.

On POSIX platforms, the default event loop is `asyncio.SelectorEventLoop`. On Windows, the default event loop on Python 3.7 and older is `asyncio.WindowsSelectorEventLoop`, but on Python 3.8 and newer the default event loop is `asyncio.WindowsProactorEventLoop` which is incompatible with `AsyncioSelectorReactor`. Applications that use `AsyncioSelectorReactor` on Windows with Python 3.8+ must call `asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())` before instantiating and running `AsyncioSelectorReactor`.

Method | `__init__` | Undocumented
Method | `addReader` | I add reader to the set of file descriptors to get read events for.
Method | `addWriter` | I add writer to the set of file descriptors to get write events for.
Method | `callFromThread` | See `twisted.internet.interfaces.IReactorFromThreads.callFromThread`.
Method | `callLater` | See twisted.internet.interfaces.IReactorTime.callLater.
Method | `crash` | See twisted.internet.interfaces.IReactorCore.crash.
Method | `getReaders` | Return the list of file descriptors currently monitored for input events by the reactor.
Method | `getWriters` | Return the list file descriptors currently monitored for output events by the reactor.
Method | `iterate` | See twisted.internet.interfaces.IReactorCore.iterate.
Method | `removeAll` | Remove all readers and writers.
Method | `removeReader` | Removes an object previously added with `addReader`.
Method | `removeWriter` | Removes an object previously added with `addWriter`.
Method | `run` | Fire 'startup' System Events, move the reactor to the 'running' state, then run the main loop until it is stopped with `stop()` or `crash()`.
Method | `stop` | See twisted.internet.interfaces.IReactorCore.stop.
Method | `_moveCallLaterSooner` | Undocumented
Method | `_onTimer` | Undocumented
Method | `_readOrWrite` | Undocumented
Method | `_reschedule` | Undocumented
Method | `_unregisterFDInAsyncio` | Compensate for a bug in asyncio where it will not unregister a FD that it cannot handle in the epoll loop. It touches internal asyncio code.
Class Variable | `_asyncClosed` | Undocumented
Class Variable | `_log` | Undocumented
Instance Variable | `_asyncioEventloop` | Undocumented
Instance Variable | `_continuousPolling` | Undocumented
Instance Variable | `_justStopped` | Undocumented
Instance Variable | `_readers` | Undocumented
Instance Variable | `_scheduledAt` | Undocumented
Instance Variable | `_timerHandle` | Undocumented
Instance Variable | `_writers` | Undocumented

Inherited from `PosixReactorBase`:

Method | `adoptDatagramPort` | Add an existing listening SOCK_DGRAM socket to the reactor to monitor for read and write readiness.
Method | `adoptStreamConnection` | No summary
Method | `adoptStreamPort` | Create a new `IListeningPort` from an already-initialized socket.
Method | `connectSSL` | Connect a client Protocol to a remote SSL socket.
Method | `connectTCP` | Connect a TCP client.
Method | `connectUNIX` | Connect a client protocol to a UNIX socket.
Method | `connectUNIXDatagram` | Connects a `ConnectedDatagramProtocol` instance to a path.
Method | `installWaker` | Install a `waker' to allow threads and signals to wake up the IO thread.
Method | `listenMulticast` | Connects a given DatagramProtocol to the given numeric UDP port.
Method | `listenSSL` | Connects a given protocol factory to the given numeric TCP/IP port. The connection is a SSL one, using contexts created by the context factory.
Method | `listenTCP` | Connects a given protocol factory to the given numeric TCP/IP port.
Method | `listenUDP` | Connects a given `DatagramProtocol` to the given numeric UDP port.
Method | `listenUNIX` | Listen on a UNIX socket.
Method | `listenUNIXDatagram` | Connects a given `DatagramProtocol` to the given path.
Method | `spawnProcess` | Spawn a process, with a process protocol.
Instance Variable | `waker` | Undocumented
Method | `_removeAll` | Remove all readers and writers, and list of removed `IReadDescriptor`s and `IWriteDescriptor`s.
Method | `_signalsFactory` | Customize reactor signal handling to support child processes on POSIX platforms.
Method | `_wakerFactory` | Undocumented
Class Variable | `_supportedAddressFamilies` | Undocumented
Instance Variable | `_childWaker` | `None` or a reference to the `_SIGCHLDWaker` which is used to properly notice child process termination.

Inherited from `_DisconnectSelectableMixin` (via `PosixReactorBase`):

Method | `_disconnectSelectable` | Utility function for disconnecting a selectable.

Inherited from `ReactorBase` (via `PosixReactorBase`, `_DisconnectSelectableMixin`):

Method | `addSystemEventTrigger` | See twisted.internet.interfaces.IReactorCore.addSystemEventTrigger.
Method | `callInThread` | See `twisted.internet.interfaces.IReactorInThreads.callInThread`.
Method | `callWhenRunning` | See twisted.internet.interfaces.IReactorCore.callWhenRunning.
Method | `disconnectAll` | Disconnect every reader, and writer in the system.
Method | `doIteration` | Do one iteration over the readers and writers which have been added.
Method | `fireSystemEvent` | See twisted.internet.interfaces.IReactorCore.fireSystemEvent.
Method | `getDelayedCalls` | See `twisted.internet.interfaces.IReactorTime.getDelayedCalls`
Method | `getThreadPool` | See `twisted.internet.interfaces.IReactorThreads.getThreadPool`.
Method | `mainLoop` | Undocumented
Method | `removeSystemEventTrigger` | See twisted.internet.interfaces.IReactorCore.removeSystemEventTrigger.
Method | `resolve` | Return a Deferred that will resolve a hostname.
Method | `runUntilCurrent` | Run all pending timed calls.
Method | `sigBreak` | Handle a SIGBREAK interrupt.
Method | `sigInt` | Handle a SIGINT interrupt.
Method | `sigTerm` | Handle a SIGTERM interrupt.
Method | `startRunning` | Method called when reactor starts: do some initialization and fire startup events.
Method | `suggestThreadPoolSize` | See `twisted.internet.interfaces.IReactorThreads.suggestThreadPoolSize`.
Method | `timeout` | Determine the longest time the reactor may sleep (waiting on I/O notification, perhaps) before it must wake up to service a time-related event.
Method | `wakeUp` | Wake up the event loop.
Class Variable | `__name__` | Undocumented
Class Variable | `installed` | Undocumented
Class Variable | `seconds` | Get the current time in seconds.
Instance Variable | `running` | See `IReactorCore.running`
Instance Variable | `threadCallQueue` | Undocumented
Instance Variable | `threadpool` | Undocumented
Instance Variable | `threadpoolShutdownID` | Undocumented
Instance Variable | `usingThreads` | Undocumented
Method | `_addInternalReader` | Add a read descriptor which is part of the implementation of the reactor itself.
Method | `_cancelCallLater` | Undocumented
Method | `_initThreadPool` | Create the threadpool accessible with callFromThread.
Method | `_initThreads` | Undocumented
Method | `_insertNewDelayedCalls` | Undocumented
Method | `_makeSignalHandling` | Get an appropriate signal handling object.
Method | `_reallyStartRunning` | Method called to transition to the running state. This should happen in the during startup event trigger phase.
Method | `_removeInternalReader` | Remove a read descriptor which is part of the implementation of the reactor itself.
Method | `_stopThreadPool` | Stop the reactor threadpool. This method is only valid if there is currently a threadpool (created by `_initThreadPool`). It is not intended to be called directly; instead, it will be called by a shutdown trigger created in ...
Method | `_uninstallHandler` | Undocumented
Class Variable | `_lock` | Undocumented
Instance Variable | `_cancellations` | Undocumented
Instance Variable | `_eventTriggers` | Undocumented
Instance Variable | `_exitSignal` | See `_ISupportsExitSignalCapturing._exitSignal`
Instance Variable | `_installSignalHandlers` | A flag which indicates whether any signal handlers will be installed during startup. This includes handlers for SIGCHLD to monitor child processes, and SIGINT, SIGTERM, and SIGBREAK
Instance Variable | `_internalReaders` | Undocumented
Instance Variable | `_newTimedCalls` | Undocumented
Instance Variable | `_pendingTimedCalls` | Undocumented
Instance Variable | `_registerAsIOThread` | A flag controlling whether the reactor will register the thread it is running in as the I/O thread when it starts. If `True`, registration will be done, otherwise it will not be.
Instance Variable | `_signals` | An object which knows how to install and uninstall the reactor's signal-handling behavior.
Instance Variable | `_started` | A flag which is true from the time `reactor.run` is called until the time `reactor.run` returns. This is used to prevent calls to `reactor.run` on a running reactor. This should be replaced with an explicit state machine.
Instance Variable | `_startedBefore` | Undocumented
Instance Variable | `_stopped` | A flag which is true between paired calls to `reactor.run` and `reactor.stop`. This should be replaced with an explicit state machine.
Instance Variable | `_threadpoolStartupID` | Undocumented

Inherited from `PluggableResolverMixin` (via `PosixReactorBase`, `_DisconnectSelectableMixin`, `ReactorBase`):

Method | `installNameResolver` | See `IReactorPluggableNameResolver`.
Method | `installResolver` | See `IReactorPluggableResolver`.
Instance Variable | `resolver` | The installed `IResolverSimple`.
Property | `nameResolver` | Implementation of read-only `IReactorPluggableNameResolver.nameResolver`.
Instance Variable | `_nameResolver` | The installed `IHostnameResolver`.

def __init__(self, eventloop: `AbstractEventLoop | None` = None): (source)
overrides `twisted.internet.base.ReactorBase.__init__`

Undocumented

def addReader(self, reader): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.addReader`

I add reader to the set of file descriptors to get read events for.

Parameters
reader | An `IReadDescriptor` provider that will be checked for read events until it is removed from the reactor with `removeReader`.

def addWriter(self, writer): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.addWriter`

I add writer to the set of file descriptors to get write events for.

Parameters
writer | An `IWriteDescriptor` provider that will be checked for write events until it is removed from the reactor with `removeWriter`.

def callFromThread(self, f, *args, **kwargs): (source)
from `twisted.internet.interfaces.IReactorFromThreads`
overrides `twisted.internet.base.ReactorBase.callFromThread`

See `twisted.internet.interfaces.IReactorFromThreads.callFromThread`.

def callLater(self, seconds, f, *args, **kwargs): (source)
from `twisted.internet.interfaces.IReactorTime`
overrides `twisted.internet.base.ReactorBase.callLater`

See twisted.internet.interfaces.IReactorTime.callLater.

def crash(self): (source)
from `twisted.internet.interfaces.IReactorCore`
overrides `twisted.internet.base.ReactorBase.crash`

See twisted.internet.interfaces.IReactorCore.crash.

Reset reactor state tracking attributes and re-initialize certain state-transition helpers which were set up in `__init__` but later destroyed (through use).

def getReaders(self): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.getReaders`

Return the list of file descriptors currently monitored for input events by the reactor.

Returns
the list of file descriptors monitored for input events.

def getWriters(self): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.getWriters`

Return the list file descriptors currently monitored for output events by the reactor.

Returns
the list of file descriptors monitored for output events.

def iterate(self, timeout): (source)
from `twisted.internet.interfaces.IReactorCore`
overrides `twisted.internet.base.ReactorBase.iterate`

See twisted.internet.interfaces.IReactorCore.iterate.

def removeAll(self): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.removeAll`

Remove all readers and writers.

Should not remove reactor internal reactor connections (like a waker).

Returns
A list of `IReadDescriptor` and `IWriteDescriptor` providers which were removed.

def removeReader(self, reader): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.removeReader`

Removes an object previously added with `addReader`.

def removeWriter(self, writer): (source)
from `twisted.internet.interfaces.IReactorFDSet`
overrides `twisted.internet.base.ReactorBase.removeWriter`

Removes an object previously added with `addWriter`.

def run(self, installSignalHandlers=True): (source)
from `twisted.internet.interfaces.IReactorCore`
overrides `twisted.internet.base.ReactorBase.run`

Fire 'startup' System Events, move the reactor to the 'running' state, then run the main loop until it is stopped with `stop()` or `crash()`.

def stop(self): (source)
from `twisted.internet.interfaces.IReactorCore`
overrides `twisted.internet.base.ReactorBase.stop`

See twisted.internet.interfaces.IReactorCore.stop.

def _moveCallLaterSooner(self, tple): (source)
overrides `twisted.internet.base.ReactorBase._moveCallLaterSooner`

Undocumented

def _onTimer(self): (source)

Undocumented

def _readOrWrite(self, selectable, read): (source)

Undocumented

def _reschedule(self): (source)

Undocumented

def _unregisterFDInAsyncio(self, fd): (source)

Compensate for a bug in asyncio where it will not unregister a FD that it cannot handle in the epoll loop. It touches internal asyncio code.

A description of the bug by markrwilliams:

The `add_writer` method of asyncio event loops isn't atomic because all the Selector classes in the selector module internally record a file object before passing it to the platform's selector implementation. If the platform's selector decides the file object isn't acceptable, the resulting exception doesn't cause the Selector to un-track the file object.

The failing/hanging stdio test goes through the following sequence of events (roughly):

* The first `connection.write(intToByte(value))` call hits the asyncio reactor's `addWriter` method.

* `addWriter` calls the asyncio loop's `add_writer` method, which happens to live on `_BaseSelectorEventLoop`.

* The asyncio loop's `add_writer` method checks if the file object has been registered before via the selector's `get_key` method.

* It hasn't, so the KeyError block runs and calls the selector's register method

* Code examples that follow use EpollSelector, but the code flow holds true for any other selector implementation. The selector's register method first calls through to the next register method in the MRO

* That next method is always `_BaseSelectorImpl.register` which creates a `SelectorKey` instance for the file object, stores it under the file object's file descriptor, and then returns it.

* Control returns to the concrete selector implementation, which asks the operating system to track the file descriptor using the right API.

* The operating system refuses! An exception is raised that, in this case, the asyncio reactor handles by creating a `_ContinuousPolling` object to watch the file descriptor.

* The second `connection.write(intToByte(value))` call hits the asyncio reactor's `addWriter` method, which hits the `add_writer` method. But the loop's selector's get_key method now returns a `SelectorKey`! Now the asyncio reactor's `addWriter` method thinks the asyncio loop will watch the file descriptor, even though it won't.

_asyncClosed: `bool` = (source)

Undocumented

_log = (source)

Undocumented

_asyncioEventloop: `AbstractEventLoop` = (source)

Undocumented

_continuousPolling = (source)

Undocumented

_justStopped: `bool` = (source)
overrides `twisted.internet.base.ReactorBase._justStopped`

Undocumented

_readers: `dict[type[FileDescriptor], int]` = (source)

Undocumented

_scheduledAt = (source)

Undocumented

_timerHandle = (source)

Undocumented

_writers: `dict[type[FileDescriptor], int]` = (source)

Undocumented