File: //usr/lib/python3/dist-packages/twisted/internet/__pycache__/asyncioreactor.cpython-310.pyc
o
�b{+ � @ s� d Z ddlZddlZddlmZmZ ddlmZmZm Z ddl
mZ ddlm
Z
ddlmZ ddlmZmZmZ dd lmZ dd
lmZ ee�G dd� de��Zdd
d�ZdS )z'
asyncio-based reactor implementation.
� N)�AbstractEventLoop�get_event_loop)�Dict�Optional�Type)�implementer)�FileDescriptor)�
IReactorFDSet)�_NO_FILEDESC�PosixReactorBase�_ContinuousPolling)�Logger)�callWithLoggerc s� e Zd ZdZdZe� Zd,dee f� fdd�
Z dd� Z
d d
� Zdd� Zd
d� Z
dd� Zdd� Zdd� Zdd� Zdd� Zdd� Zd-dd�Z� fdd�Z� fd d!�Zd"d#� Zd$d%� Zd&d'� Zd(d)� Zd*d+� Z� ZS ).�AsyncioSelectorReactora�
Reactor running on top of L{asyncio.SelectorEventLoop}.
On POSIX platforms, the default event loop is
L{asyncio.SelectorEventLoop}.
On Windows, the default event loop on Python 3.7 and older
is C{asyncio.WindowsSelectorEventLoop}, but on Python 3.8 and newer
the default event loop is C{asyncio.WindowsProactorEventLoop} which
is incompatible with L{AsyncioSelectorReactor}.
Applications that use L{AsyncioSelectorReactor} on Windows
with Python 3.8+ must call
C{asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())}
before instantiating and running L{AsyncioSelectorReactor}.
FN� eventloopc sx |d u rt � }n|}tjdkr!ddlm} t||�r!td|� ���|| _i | _i | _ t
| �| _d | _d | _
t� �� d S )N�win32r )�ProactorEventLoopz)ProactorEventLoop is not supported, got: )r �sys�platform�asyncior �
isinstance� TypeError�_asyncioEventloop�_writers�_readersr �_continuousPolling�_scheduledAt�_timerHandle�super�__init__)�selfr �
_eventloopr �� __class__� �A/usr/lib/python3/dist-packages/twisted/internet/asyncioreactor.pyr 0 s
�
zAsyncioSelectorReactor.__init__c C s* z
| j j�|� W dS ty Y dS w )am
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 C{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 C{connection.write(intToByte(value))} call hits the asyncio
reactor's C{addWriter} method.
* C{addWriter} calls the asyncio loop's C{add_writer} method, which
happens to live on C{_BaseSelectorEventLoop}.
* The asyncio loop's C{add_writer} method checks if the file object has
been registered before via the selector's C{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 C{_BaseSelectorImpl.register} which
creates a C{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 C{_ContinuousPolling}
object to watch the file descriptor.
* The second C{connection.write(intToByte(value))} call hits the
asyncio reactor's C{addWriter} method, which hits the C{add_writer}
method. But the loop's selector's get_key method now returns a
C{SelectorKey}! Now the asyncio reactor's C{addWriter} method thinks
the asyncio loop will watch the file descriptor, even though it won't.
N)r � _selector�
unregister�
BaseException)r �fdr$ r$ r% �_unregisterFDInAsyncioK s
2�z-AsyncioSelectorReactor._unregisterFDInAsyncioc
C s� |r|j n|j}|�� dkr| �|t|� d S z|� }W n ty6 } z|}| j�d � W Y d }~nd }~ww |rB| �|||� d S d S �N���)�doRead�doWrite�fileno�_disconnectSelectabler
� Exception�_log�failure)r �
selectable�read�method�why�er$ r$ r% �_readOrWrite� s
���z#AsyncioSelectorReactor._readOrWritec
C s� || j �� v s
|| jj v rd S |�� }z| j�|t|| j|d� || j |<