K imUdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlmZddlmZmZddl mZmZmZmZmZmZmZmZmZmZe j:rddlmZm Z GddeZ!ee"e!fZ#ed Z$e j:red Z%e&Z'ed e(d <ddZ)ejTe)GddeZ+Gdde+Z,Gdde+Z-dej\d ej\fdZ/dej\d ej\fdZ0da1de2d e j&fdZ3Gdd Z4GddejjZ6y)akBridges between the `asyncio` module and Tornado IOLoop. .. versionadded:: 3.2 This module integrates Tornado with the ``asyncio`` module introduced in Python 3.4. This makes it possible to combine the two libraries on the same event loop. .. deprecated:: 5.0 While the code in this module is still used, it is now enabled automatically when `asyncio` is available, so applications should no longer need to refer to this module directly. .. note:: Tornado is designed to use a selector-based event loop. On Windows, where a proactor-based event loop has been the default since Python 3.8, a selector event loop is emulated by running ``select`` on a separate thread. Configuring ``asyncio`` to use a selector event loop may improve performance of Tornado (but may reduce performance of other ``asyncio``-based libraries in the same process). Nconvert_yielded)IOLoop _Selectable) AnyCallableDictListOptionalProtocolSetTupleTypeVarUnion) TypeVarTupleUnpackceZdZdefdZy) _HasFilenoreturncyNselfs ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/platform/asyncio.pyfilenoz_HasFileno.fileno:s N)__name__ __module__ __qualname__intrrrrrr9s  rr_T_TsSelectorThread_selector_loopsrcjtD]{}|j5d|_|jjddd |jj d|jb|jj}tjy#1swYcxYw#t $rYUwxYw)NTa) r% _select_cond_closing_selectornotify_waker_wsendBlockingIOError_threadjoinclear)loops r_atexit_callbackr2Is      '%)D "    $ $ & '  MM  t $ << # LL      ' '    s"BB&B# & B21B2c eZdZdejdeddffd Zd deddfdZde e e fd e d d e ddfd Z de e e fd e ddfd Zde e e fddfdZde d e ddfdZd!dZd!dZdede dededef dZdeddfdZde dededdfdZde dededdfdZdeej6j8de defdeddfdZdej6j8ddfdZxZ S)"BaseAsyncIOLoop asyncio_loopkwargsrNc ||_||_ttdr*t |tj rt ||_i|_t|_ t|_ d|_ tjjD]%}|js tj|='tjj#||}||urt%d|d|t'|Pdi|y#t $rYxwxYw)NProactorEventLoopFzIOLoop z& already associated with asyncio loop r)r5 selector_loophasattrasyncio isinstancer8AddThreadSelectorEventLoophandlerssetreaderswritersclosingr_ioloop_for_asynciocopy is_closedKeyError setdefault RuntimeErrorsuper initialize)rr5r6r1 existing_loop __class__s rrJzBaseAsyncIOLoop.initialize_s )* 7/ 0Z '336 "remove_handlerclose_fdrrCr5r9close)rrMfdfileobj handler_funcs rrSzBaseAsyncIOLoop.closes t}}% 'B$(MM"$5 !G\    # g&  '  & &t'8'8 9   T%6%6 6    $ $ & !rrThandler.Neventsc|j|\}}||jvrtd|z||f|j|<|tjzrQ|j j ||j|tj|jj||tjzrR|j j||j|tj|jj|yy)Nzfd %s added twice) split_fdr> ValueErrorrREADr9 add_reader_handle_eventsr@addWRITE add_writerrA)rrTrWrYrUs r add_handlerzBaseAsyncIOLoop.add_handlersmmB' G  0256 6$g. b FKK     ) )"d.A.A2v{{ S LL  R FLL    ) )"d.A.A2v|| T LL  R  !rc |j|\}}|tjzr`||jvr|jj ||j |tj|jj|nD||jvr6|jj||jj||tjzra||jvrR|jj||j |tj|jj|yy||jvr7|jj||jj|yyr)r[rr]r@r9r^r_r` remove_readerremoverarArb remove_writer)rrTrYrUs rupdate_handlerzBaseAsyncIOLoop.update_handlers mmB' G FKK %""--b$2E2Er6;;W   $T\\!""004 ##B' FLL %""--b$2E2Er6<<X   $&T\\!""004 ##B'"rct|j|\}}||jvry||jvr6|jj ||jj |||j vr6|jj||j j ||j|=yr)r[r>r@r9rerfrArg)rrTrUs rrQzBaseAsyncIOLoop.remove_handlersmmB' G T]] "       , ,R 0 LL   #      , ,R 0 LL   # MM" rc:|j|\}}|||yr)r>)rrTrYrUrVs rr_zBaseAsyncIOLoop._handle_eventss $ b 1Wf%rc8|jjyr)r5 run_foreverrs rstartzBaseAsyncIOLoop.starts %%'rc8|jjyr)r5stoprs rrozBaseAsyncIOLoop.stops  rwhencallbackargsc |jjtd||jz |jt j |g|i|S)Nr)r5 call_latermaxtime _run_callback functoolspartial)rrprqrrr6s rcall_atzBaseAsyncIOLoop.call_atsS   ++ 4$))+% &      h 8 8 8  rtimeoutc$|jyr)cancel)rr{s rremove_timeoutzBaseAsyncIOLoop.remove_timeouts rcr tj|jur|jj}n|jj} ||j tj|g|i|y#t $r|jj}YMwxYw#t $rYyt$rYywxYwr) r;get_running_loopr5 call_sooncall_soon_threadsaferHrwrxryAttributeError)rrqrrr6rs r add_callbackzBaseAsyncIOLoop.add_callbacks ?'')T->->> --77  --BB   d(()*;*;H*Vt*Vv*V W  ?))>>I ?       s*A A;*B ;BB B6+B65B6ctjdt |jj |j t j|g|i|y#t$rYywxYw)Nz&add_callback_from_signal is deprecated) warningswarnDeprecationWarningr5rrwrxryrH)rrqrrr6s radd_callback_from_signalz(BaseAsyncIOLoop.add_callback_from_signalsa  >@RS     2 2""I$5$5h$P$P$P    s=A A&%A&executorfunc.zasyncio.Future[_T]c>|jj||g|Sr)r5run_in_executor)rrrrrs rrzBaseAsyncIOLoop.run_in_executor s$ 1t  004G$GGrc8|jj|Sr)r5set_default_executor)rrs rrz$BaseAsyncIOLoop.set_default_executors  55h??rFrN)!rrr r;AbstractEventLooprrJboolrSrr!rrrcrhrQr_rmrofloatobjectrzr~rrr concurrentfuturesExecutorr"rr __classcell__rLs@rr4r4^s/%#55/%AD/% /%b"T"d"" ![() !4v>rcyrrrs r _make_currentzAsyncIOMainLoop._make_current(s rr)rrr __doc__rrJrrrs@rrrs  ?3?4? rrcReZdZdZdeddffd Zd deddffd Zd dZd d Z xZ S) AsyncIOLoopaa``AsyncIOLoop`` is an `.IOLoop` that runs on an ``asyncio`` event loop. This class follows the usual Tornado semantics for creating new ``IOLoops``; these loops are not necessarily related to the ``asyncio`` default event loop. Each ``AsyncIOLoop`` creates a new ``asyncio.EventLoop``; this object can be accessed with the ``asyncio_loop`` attribute. .. versionchanged:: 6.2 Support explicit ``asyncio_loop`` argument for specifying the asyncio loop to attach to, rather than always creating a new one with the default policy. .. versionchanged:: 5.0 When an ``AsyncIOLoop`` becomes the current `.IOLoop`, it also sets the current `asyncio` event loop. .. deprecated:: 5.0 Now used automatically when appropriate; it is no longer necessary to refer to this class directly. r6rNc d|_d}d|vrtjx|d<} t|di|y#t $r||j wxYw)NFr5r) is_currentr;new_event_looprIrJ ExceptionrS)rr6r1rLs rrJzAsyncIOLoop.initializeHsg  ',3,B,B,D DF> "T  G  ( (    s 9ArMc^|jr|jt| |y)N)rM)r_clear_currentrIrS)rrMrLs rrSzAsyncIOLoop.closeVs$ ??    !  g &rc|js! tj|_d|_tj |jy#tt f$r d|_Y?wxYwrO)rr;r old_asynciorHAssertionErrorset_event_loopr5rs rrzAsyncIOLoop._make_current[sZ (#*#9#9#; #DOt001!.1 (#'  (sAA'&A'cj|jr'tj|jd|_yy)NF)rr;rrrs r_clear_current_hookzAsyncIOLoop._clear_current_hookds) ??  " "4#3#3 4#DO rrr) rrr rrrJrrSrrrrs@rrr.s82 3 4 'T'd' 2$rrasyncio_futurec|S)zConvert an `asyncio.Future` to a `tornado.concurrent.Future`. .. versionadded:: 4.1 .. deprecated:: 5.0 Tornado ``Futures`` have been merged with `asyncio.Future`, so this method is now a no-op. r)rs rto_tornado_futurerjs  rtornado_futurect|S)asConvert a Tornado yieldable object to an `asyncio.Future`. .. versionadded:: 4.1 .. versionchanged:: 4.3 Now accepts any yieldable object, not just `tornado.concurrent.Future`. .. deprecated:: 5.0 Tornado ``Futures`` have been merged with `asyncio.Future`, so this method is now equivalent to `tornado.gen.convert_yielded`. r)rs rto_asyncio_futurervs > **rnamec|dk7rtdtd|tQtjdk(r!t t drt j}nt j}Gdd|}|atS)NAnyThreadEventLoopPolicyzmodule z has no attribute win32WindowsSelectorEventLoopPolicycHeZdZdZdfd Zdej ffd ZxZS)-__getattr__..AnyThreadEventLoopPolicyaEvent loop policy that allows loop creation on any thread. The default `asyncio` event loop policy only automatically creates event loops in the main threads. Other threads must create event loops explicitly or `asyncio.get_event_loop` (and therefore `.IOLoop.current`) will fail. Installing this policy allows event loops to be created automatically on any thread, matching the behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2). Usage:: asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy()) .. versionadded:: 5.0 .. deprecated:: 6.2 ``AnyThreadEventLoopPolicy`` affects the implicit creation of an event loop, which is deprecated in Python 3.10 and will be removed in a future version of Python. At that time ``AnyThreadEventLoopPolicy`` will no longer be useful. If you are relying on it, use `asyncio.new_event_loop` or `asyncio.run` explicitly in any non-main threads that need event loops. rcZt|tjdtdy)NzYAnyThreadEventLoopPolicy is deprecated, use asyncio.run or asyncio.new_event_loop instead) stacklevel)rI__init__rrr)rrLs rrz6__getattr__..AnyThreadEventLoopPolicy.__init__s% " 8& rc t|S#t$r&|j}|j ||cYSwxYwr)rIrrHrr)rr1rLs rrz<__getattr__..AnyThreadEventLoopPolicy.get_event_loopsF  7133# ..0D''-K  s ,AAr) rrr rrr;rrrrs@rrrs$ 4  (A(A  r) rr_AnyThreadEventLoopPolicysysplatformr:r;rDefaultEventLoopPolicy)r _BasePolicyrs r __getattr__rsz  ))wxl2DTHMNN!( <<7 "w 5( "@@K!88K+ {+ Z%=! $$rc eZdZdZdZdej ddfdZddZde jdfd Z dd Z dd Z dd Zdd ZdeedeeddfdZdedeeefddfdZdededdeddfdZdededdeddfdZdedefdZdedefdZy)r$aqDefine ``add_reader`` methods to be called in a background select thread. Instances of this class start a second thread to run a selector. This thread is completely hidden from the user; all callbacks are run on the wrapped event loop's thread. Typically used via ``AddThreadSelectorEventLoop``, but can be attached to a running asyncio loop. F real_looprNcptj_|_t j _d_d_d_ j_ dfd jjfdji_ i_tj \__j"j'dj$j'dt(j+j-j"j.y)NFcVKjjd{y7wr)_thread_manager_handle __anext__rsrthread_manager_anextz5SelectorThread.__init__..thread_manager_anexts!--779 9 9s )')cDjjSr) _real_loop create_task)rrsrz)SelectorThread.__init__..sDOO//0D0FGrcontextr) contextvars copy_context_main_thread_ctxr threading Conditionr( _select_argsr)r._thread_managerrr_readers_writerssocket socketpair_waker_rr+ setblockingr%r`r^_consume_waker)rrrs` @rrzSelectorThread.__init__s + 8 8 :#%//1  "'37 &*&:&:&<# : !! G)) " >@ =? (.'8'8':$ t} !!%( !!%(D!  t':':;rc|jry|j5d|_|jjddd|j |j |j j tj||j|j|jj|jjd|_y#1swYxYwrO) _closedr(r)r*_wake_selectorr.r/r%discardrerrSr+rs rrSzSelectorThread.closes <<     '%)D "    $ $ & '  << # LL   % 4==)    ' 's "C&&C/)NNcKtjdd|j|_|jj |j  dy#t $r|jwxYww)NzTornado selectorT)rdaemontarget)rThread _run_selectr.rm _start_select GeneratorExitrSrs rrzSelectorThread._thread_managersk !''###       JJL  sAA9AA9A66A9ct|jry |jjdy#t$rYywxYw)Nr')rr+r,r-rs rrzSelectorThread._wake_selector(s5 <<   MM  t $   s + 77cZ |jjdy#t$rYywxYw)Ni)rrecvr-rs rrzSelectorThread._consume_waker0s+  MM  t $   s  **c,|j5|jJt|jj t|j j f|_|jj dddy#1swYyxYwr)r(rrPrkeysrr*rs rrzSelectorThread._start_select6sw   '$$, ,,!%dmm&8&8&:!;T$--BTBTBV=W XD     $ $ & ' ' 's A4B  Bc* |j5|j?|js3|jj|j |js3|jr dddy|jJ|j\}}d|_ddd t j|\}}}||z} |jj|j|||j#1swY_xYw#t $rr}|j tt dt jk(r;t j|jjgggd\}}}|rg}nYd}~d}~wwxYw#t$rYt $rYwxYw)N WSAENOTSOCKrr)r(rr)waitselectOSErrorerrnogetattrEBADFrrrr_handle_selectrrHr)rto_readto_writerswsxse_s rrzSelectorThread._run_select?s"" )''/8N8N%%**,''/8N8N))  ) ) ((444$($5$5!$(! )  $]]7HhG B"W, 44''R9N9N5c ) )4 77ge]EKKHH%}}dmm.B.B.D-Er2qQHB1  2   !   sIA C2 C20$C2 C>>3E<2C;> E9A(E44E9< FFFrrc|D]}|j||j |D]}|j||j |jyr) _handle_eventrrr)rrrrws rrzSelectorThread._handle_selectsV 1A   q$-- 0 1 1A   q$-- 0 1 rrTcb_mapc< ||}|y#t$rYywxYwr)rF)rrTrrqs rrzSelectorThread._handle_events+  bzH     s  rqrXrrcjtj|g||j|<|jyr)rxryrrrrTrqrrs rr^zSelectorThread.add_reader.&--h>> b rcjtj|g||j|<|jyr)rxryrrr s rrbzSelectorThread.add_writerr rc^ |j|=|jy#t$rYywxYwNFT)rrFrrrTs rrezSelectorThread.remove_reader8  b!    ,,c^ |j|=|jy#t$rYywxYwr)rrFrrs rrgzSelectorThread.remove_writerrrr)rrr rrr;rrrStypingAsyncGeneratorrrrrrr _FileDescriptorLikerr rrrr^rbrrergrrrr$r$s+G#<'";";#<#