/ lib / tornado / platform / asyncio.pyc
asyncio.pyc
  1  o

  2  ��c�f�@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
  3  Z
  4  ddlZddlZddl
mZddlmZmZddlmZmZmZmZmZmZmZmZmZejroddlmZddlmZGdd	�d	e�Zee efZ!ed
  5  �Z"e#�Z$d$dd
�Z%e�&e%�e	j'dkr�dej(fdd�Z)nddlm*Z)Gdd�de�Z+Gdd�de+�Z,Gdd�de+�Z-dej.dej.fdd�Z/dej.dej.fdd�Z0e	j1dkr�e2ed�r�ej3Z4nej5Z4Gd d!�d!e4�Z6Gd"d#�d#ej(�Z7dS)%akBridges between the `asyncio` module and Tornado IOLoop.
  6  
  7  .. versionadded:: 3.2
  8  
  9  This module integrates Tornado with the ``asyncio`` module introduced
 10  in Python 3.4. This makes it possible to combine the two libraries on
 11  the same event loop.
 12  
 13  .. deprecated:: 5.0
 14  
 15     While the code in this module is still used, it is now enabled
 16     automatically when `asyncio` is available, so applications should
 17     no longer need to refer to this module directly.
 18  
 19  .. note::
 20  
 21     Tornado is designed to use a selector-based event loop. On Windows,
 22     where a proactor-based event loop has been the default since Python 3.8,
 23     a selector event loop is emulated by running ``select`` on a separate thread.
 24     Configuring ``asyncio`` to use a selector event loop may improve performance
 25     of Tornado (but may reduce performance of other ``asyncio``-based libraries
 26     in the same process).
 27  �N��convert_yielded)�IOLoop�_Selectable)	�Any�TypeVar�	Awaitable�Callable�Union�Optional�List�Tuple�Dict)�Set)�Protocolc@seZdZdefdd�ZdS)�
 28  _HasFileno�returncC�dS�N���selfrr��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tornado\platform\asyncio.py�fileno.sz_HasFileno.filenoN)�__name__�
 29  __module__�__qualname__�intrrrrrr-sr�_Trc	CsztD]4}|j�d|_|j��Wd�n1swYz|j�d�Wn	ty0Ynw|j��qt�	�dS)NT�a)
 30  �_selector_loops�_select_cond�_closing_selector�notify�_waker_w�send�BlockingIOError�_thread�join�clear)�looprrr�_atexit_callback:s��r+)��
 31  cCs*zt��WStyYnwt����Sr)�asyncio�get_running_loop�RuntimeError�get_event_loop_policy�get_event_looprrrr�_get_event_loopOs
 32  �r3)r2c
 33  speZdZdejdeddf�fdd�Zd.deddfd	d
 34  �Zde	e
 35  efded
de
 36  ddfdd�Z
de	e
 37  efde
 38  ddfdd�Zde	e
 39  efddfdd�Zde
 40  de
 41  ddfdd�Zd/dd�Zd/dd�Zdededededef
 42  dd�Zd eddfd!d"�Zdedededdfd#d$�Zdedededdfd%d&�Zd'eejjd(ed)efdedeefd*d+�Zd'ejjddfd,d-�Z �Z!S)0�BaseAsyncIOLoop�asyncio_loop�kwargsrNc	s�||_||_ttd�rt|tj�rt|�|_i|_t�|_	t�|_
 43  d|_tj
��D]}|��r?ztj
|=Wq)ty>Yq)wq)tj
�||�}||urUtd|�d|����t�jdi|��dS)N�ProactorEventLoopFzIOLoop z& already associated with asyncio loop r)r5�
selector_loop�hasattrr.�
 44  isinstancer7�AddThreadSelectorEventLoop�handlers�set�readers�writers�closingr�_ioloop_for_asyncio�copy�	is_closed�KeyError�
 45  setdefaultr0�super�
 46  initialize)rr5r6r*Z
existing_loop��	__class__rrrG]s0�
 47  ���zBaseAsyncIOLoop.initializeF�all_fdscCsjd|_t|j�D]}|j|\}}|�|�|r|�|�qtj|j=|j|jur.|j�	�|j�	�dS�NT)
 48  r@�listr<�remove_handler�close_fdrrAr5r8�close)rrJ�fd�fileobj�handler_funcrrrrO�s
 49  
 50  �
 51  
 52  zBaseAsyncIOLoop.closerP�handler�.N�eventscCs�|�|�\}}||jvrtd|��||f|j|<|tj@r/|j�||j|tj�|j�	|�|tj
 53  @rG|j�||j|tj
 54  �|j�	|�dSdS)Nzfd %s added twice)
�split_fdr<�
 55  ValueErrorr�READr8�
 56  add_reader�_handle_eventsr>�add�WRITE�
 57  add_writerr?)rrPrSrUrQrrr�add_handler�s
 58  
 59  
 60  �zBaseAsyncIOLoop.add_handlercCs�|�|�\}}|tj@r#||jvr"|j�||j|tj�|j�|�n||jvr4|j�|�|j�	|�|tj
 61  @rS||jvrQ|j�||j|tj
 62  �|j�|�dSdS||jvrf|j�
|�|j�	|�dSdSr)rVrrXr>r8rYrZr[�
remove_reader�remover\r?r]�
remove_writer)rrPrUrQrrr�update_handler�s$
 63  
 64  �
 65  
 66  
 67  �
 68  �zBaseAsyncIOLoop.update_handlercCsl|�|�\}}||jvrdS||jvr|j�|�|j�|�||jvr0|j�|�|j�|�|j|=dSr)rVr<r>r8r_r`r?ra)rrPrQrrrrM�s
 69  
 70  
 71  zBaseAsyncIOLoop.remove_handlercCs|j|\}}|||�dSr)r<)rrPrUrQrRrrrrZ�szBaseAsyncIOLoop._handle_eventsc	CsZzt�}Wn
ttfyd}Ynwzt�|j�|j��Wt�|�dSt�|�wr)r3r0�AssertionErrorr.�set_event_loopr5�run_forever)r�old_looprrr�start�s
 72  �zBaseAsyncIOLoop.startcCs|j��dSr)r5�stoprrrrrh�szBaseAsyncIOLoop.stop�when�callback�argscOs4|j�td||���|jtj|g|�Ri|���S)Nr)r5�
 73  call_later�max�time�
_run_callback�	functools�partial)rrirjrkr6rrr�call_at�s
 74  �zBaseAsyncIOLoop.call_at�timeoutcCs|��dSr)�cancel)rrsrrr�remove_timeout��zBaseAsyncIOLoop.remove_timeoutcOs�zt��|jur
|jj}n|jj}Wn
ty|jj}Ynwz||jtj|g|�Ri|���WdSty=YdSt	yFYdSwr)
 75  r.r/r5�	call_soon�call_soon_threadsafer0rorprq�AttributeError)rrjrkr6rwrrr�add_callback�s
 76  ��&�zBaseAsyncIOLoop.add_callbackcOs@z|j�|jtj|g|�Ri|���WdStyYdSwr)r5rxrorprqr0)rrjrkr6rrr�add_callback_from_signals
 77  ��z(BaseAsyncIOLoop.add_callback_from_signal�executor�func.cGs|jj||g|�R�Sr)r5�run_in_executor)rr|r}rkrrrr~szBaseAsyncIOLoop.run_in_executorcCs|j�|�Sr)r5�set_default_executor)rr|rrrrrvz$BaseAsyncIOLoop.set_default_executor�F�rN)"rrrr.�AbstractEventLooprrG�boolrOr
 78  rrr	r^rbrMrZrgrh�float�objectrrrurzr{r�
 79  concurrent�futures�Executorrrr~r�
__classcell__rrrHrr4\sn���1
 80  ���
 81  �
 82  
 83  ����
 84  ����
 85  �
 86  
 87  �
 88  ��
 89  �r4cs4eZdZdZdeddf�fdd�Zd	dd�Z�ZS)
 90  �AsyncIOMainLoopa�``AsyncIOMainLoop`` creates an `.IOLoop` that corresponds to the
 91      current ``asyncio`` event loop (i.e. the one returned by
 92      ``asyncio.get_event_loop()``).
 93  
 94      .. deprecated:: 5.0
 95  
 96         Now used automatically when appropriate; it is no longer necessary
 97         to refer to this class directly.
 98  
 99      .. versionchanged:: 5.0
100  
101         Closing an `AsyncIOMainLoop` now closes the underlying asyncio loop.
102      r6rNcst�jt��fi|��dSr)rFrGr.r2)rr6rHrrrG*szAsyncIOMainLoop.initializecCrrrrrrr�make_current-szAsyncIOMainLoop.make_currentr�)rrr�__doc__rrGr�r�rrrHrr�sr�csVeZdZdZdeddf�fdd�Zddeddf�fd	d
103  �
Zddd�Zdd
d�Z	�Z
104  S)�AsyncIOLoopaa``AsyncIOLoop`` is an `.IOLoop` that runs on an ``asyncio`` event loop.
105      This class follows the usual Tornado semantics for creating new
106      ``IOLoops``; these loops are not necessarily related to the
107      ``asyncio`` default event loop.
108  
109      Each ``AsyncIOLoop`` creates a new ``asyncio.EventLoop``; this object
110      can be accessed with the ``asyncio_loop`` attribute.
111  
112      .. versionchanged:: 6.2
113  
114         Support explicit ``asyncio_loop`` argument
115         for specifying the asyncio loop to attach to,
116         rather than always creating a new one with the default policy.
117  
118      .. versionchanged:: 5.0
119  
120         When an ``AsyncIOLoop`` becomes the current `.IOLoop`, it also sets
121         the current `asyncio` event loop.
122  
123      .. deprecated:: 5.0
124  
125         Now used automatically when appropriate; it is no longer necessary
126         to refer to this class directly.
127      r6rNcs\d|_d}d|vrt��|d<}zt�jdi|��WdSty-|dur,|���w)NFr5r)�
128  is_currentr.�new_event_looprFrG�	ExceptionrO)rr6r*rHrrrGMs�zAsyncIOLoop.initializeFrJcs |jr|��t�j|d�dS)N)rJ)r��_clear_currentrFrO)rrJrHrrrO[szAsyncIOLoop.closec	CsXtjdtdd�|js$zt��|_Wnttfy d|_Ynwd|_t�	|j
129  �dS)Nz6make_current is deprecated; start the event loop first���
130  stacklevelT)�warnings�warn�DeprecationWarningr�r.r2�old_asyncior0rcrdr5rrrrr�`s�
131  �zAsyncIOLoop.make_currentcCs |jrt�|j�d|_dSdS)NF)r�r.rdr�rrrr�_clear_current_hookns
132  �zAsyncIOLoop._clear_current_hookr�r�)rrrr�rrGr�rOr�r�r�rrrHrr�3s
133  r��asyncio_futurecCs|S)z�Convert an `asyncio.Future` to a `tornado.concurrent.Future`.
134  
135      .. versionadded:: 4.1
136  
137      .. deprecated:: 5.0
138         Tornado ``Futures`` have been merged with `asyncio.Future`,
139         so this method is now a no-op.
140      r)r�rrr�to_tornado_futurets	r��tornado_futurecCst|�S)asConvert a Tornado yieldable object to an `asyncio.Future`.
141  
142      .. versionadded:: 4.1
143  
144      .. versionchanged:: 4.3
145         Now accepts any yieldable object, not just
146         `tornado.concurrent.Future`.
147  
148      .. deprecated:: 5.0
149         Tornado ``Futures`` have been merged with `asyncio.Future`,
150         so this method is now equivalent to `tornado.gen.convert_yielded`.
151      r)r�rrr�to_asyncio_future�s
r��win32�WindowsSelectorEventLoopPolicycs6eZdZdZd�fdd�Zdejf�fdd�Z�ZS)	�AnyThreadEventLoopPolicya�Event loop policy that allows loop creation on any thread.
152  
153      The default `asyncio` event loop policy only automatically creates
154      event loops in the main threads. Other threads must create event
155      loops explicitly or `asyncio.get_event_loop` (and therefore
156      `.IOLoop.current`) will fail. Installing this policy allows event
157      loops to be created automatically on any thread, matching the
158      behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).
159  
160      Usage::
161  
162          asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
163  
164      .. versionadded:: 5.0
165  
166      .. deprecated:: 6.2
167  
168          ``AnyThreadEventLoopPolicy`` affects the implicit creation
169          of an event loop, which is deprecated in Python 3.10 and
170          will be removed in a future version of Python. At that time
171          ``AnyThreadEventLoopPolicy`` will no longer be useful.
172          If you are relying on it, use `asyncio.new_event_loop`
173          or `asyncio.run` explicitly in any non-main threads that
174          need event loops.
175      rNcst���tjdtdd�dS)NzYAnyThreadEventLoopPolicy is deprecated, use asyncio.run or asyncio.new_event_loop insteadr�r�)rF�__init__r�r�r�rrHrrr��s
176  
177  �z!AnyThreadEventLoopPolicy.__init__c	s:zt���WSttfy|��}|�|�|YSwr)rFr2r0rcr�rd)rr*rHrrr2�s
178  �z'AnyThreadEventLoopPolicy.get_event_loopr�)	rrrr�r�r.r�r2r�rrrHrr��s	r�cseZdZdZhd�Zdedef�fdd�Zdej	ddfd	d
179  �Z
180  d+dd�Zd+d
d�Zd+dd�Z
d+dd�Zd+dd�Zd+dd�Zdeddedddfdd�Zdddedefddfdd�Zddd ed!d"eddfd#d$�Zddd ed!d"eddfd%d&�Zd,d'd(�Zd,d)d*�Z�ZS)-r;a�Wrap an event loop to add implementations of the ``add_reader`` method family.
181  
182      Instances of this class start a second thread to run a selector.
183      This thread is completely hidden from the user; all callbacks are
184      run on the wrapped event loop's thread.
185  
186      This class is used automatically by Tornado; applications should not need
187      to refer to it directly.
188  
189      It is safe to wrap any event loop with this class, although it only makes sense
190      for event loops that do not implement the ``add_reader`` family of methods
191      themselves (i.e. ``WindowsProactorEventLoop``)
192  
193      Closing the ``AddThreadSelectorEventLoop`` also closes the wrapped event loop.
194  
195      >�_handle_selectr"�
_handle_event�
196  _real_loop�_consume_waker�_writers�_waker_rr'r]�_select_argsr$�
_start_selectr_�_readers�_wake_selector�_run_selectrOrYr!ra�namercs"|tjvrt��|�St|j|�Sr)r;�
MY_ATTRIBUTESrF�__getattribute__�getattrr�)rr�rHrrr��s
197  z+AddThreadSelectorEventLoop.__getattribute__�	real_loopNcCs�||_t��|_d|_d|_tjdd|jd�|_|j�	�|j�
198  |j�i|_i|_
t��\|_|_|j�d�|j�d�t�|�|�|j|j�dS)NFzTornado selectorT)r��daemon�target)r��	threading�	Conditionr!r�r"�Threadr�r'rgrwr�r�r��socket�
199  socketpairr�r$�setblockingr r[rYr�)rr�rrrr��s&
200  ��
201  
202  z#AddThreadSelectorEventLoop.__init__cCs"t�|�|j��|j��dSr)r �discardr�rOr$rrrr�__del__s
203  
204  z"AddThreadSelectorEventLoop.__del__cCst|j�d|_|j��Wd�n1swY|��|j��t�|�|j�	�|j
205  �	�|j�	�dSrK)r!r"r#r�r'r(r r�r�rOr$r�rrrrrO#s�
206  
207  
208  
209  z AddThreadSelectorEventLoop.closecC�(z	|j�d�WdStyYdSw)Nr)r$r%r&rrrrr�.�
210  �z)AddThreadSelectorEventLoop._wake_selectorcCr�)Ni)r��recvr&rrrrr�4r�z)AddThreadSelectorEventLoop._consume_wakercCsb|j�$|jdusJ�t|j���t|j���f|_|j��Wd�dS1s*wYdSr)r!r�rLr��keysr�r#rrrrr�:s
211  "�z(AddThreadSelectorEventLoop._start_selectc
212  CsJ	|j�8|jdur|js|j��|jdur|jr
|jr&	Wd�dS|jdus-J�|j\}}d|_Wd�n1s?wYzt�|||�\}}}||}Wn2ty�}z&|jttdtj�kr{t�|j	�
213  �gggd�\}}}|rzg}n��WYd}~nd}~wwz|j�|j
||�Wnty�Yn	ty�Ynwq)NTZWSAENOTSOCKr)r!r�r"�wait�select�OSError�errnor��EBADFr�rr�rxr�r0ry)r�to_read�to_write�rs�ws�xs�e�_rrrr�CsB
214  ��
215  ������z&AddThreadSelectorEventLoop._run_selectr��_FileDescriptorLiker�cCs<|D]	}|�||j�q|D]	}|�||j�q|��dSr)r�r�r�r�)rr�r��r�wrrrr��s
216  z)AddThreadSelectorEventLoop._handle_selectrP�cb_mapcCs,z||}Wn
217  tyYdSw|�dSr)rD)rrPr�rjrrrr��s�
218  z(AddThreadSelectorEventLoop._handle_eventrjrTrkcG�$tj|g|�R�|j|<|��dSr)rprqr�r��rrPrjrkrrrrY��z%AddThreadSelectorEventLoop.add_readercGr�r)rprqr�r�r�rrrr]�r�z%AddThreadSelectorEventLoop.add_writercC�|j|=|��dSr)r�r��rrPrrrr_��z(AddThreadSelectorEventLoop.remove_readercCr�r)r�r�r�rrrra�r�z(AddThreadSelectorEventLoop.remove_writerr�)rPr�rN)rrrr�r��strrr�r.r�r�r�rOr�r�r�r�rr�rr	r�rYr]r_rar�rrrHrr;�sZ
219  !
220  
221  
222  
223  
224  
225  	@��
226  �	�
227  �
228  ����
229  ����
230231  r;r�)8r�r.�atexit�concurrent.futuresr�r�rpr�r��sysr��typingr��tornado.genr�tornado.iolooprrrrrr	r
232  rrr
r�
TYPE_CHECKINGr�typing_extensionsrrrr�rr=r r+�register�version_infor�r3r2r4r�r��Futurer�r��platformr9r��_BasePolicy�DefaultEventLoopPolicyr�r;rrrr�<module>sL,
233  
234  
235  
236  @A0