fdZdZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddlm Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZej&dj(Zd/d Zd/d ZdZGddej2ZeZ ddlZej4xZZddddZej"j@Z ej"jBZ!ej"jDZ"de"ddZ#dZ$dZ%dZ&dZ'dddZ(ejRdZ*d/dZ+dddZ,Gdd ejZZ.d!d"d#Z/d$Z0d%Z1d&Z2e2eZ3e jhZ5e6Z7iZ8d'Z9d(Z:d)Z;d*Zd-Z?eZ@e9ZAe:ZBe>ZCe?ZDe;ZEeZ>m?Z?m;Z;mZKe?ZLe;ZMe!  #t+AFFH  >>   FADy   >sB0B"BBc| |j}||yy#t$rtjdtdYywxYw)Nz~Task.set_name() was added in Python 3.8, the method support will be mandatory for third-party task implementations since 3.13.) stacklevel)set_nameAttributeErrorwarningswarnDeprecationWarning)tasknamer8s r&_set_task_namer?FsM  }}H TN 8 MM9)Q 8 8s %AAceZdZdZdZdddddfd ZfdZeeZ dZ d Z d Z d Z d Zd ZdZdddZddddZddZdZdZdZddZfdZdZxZS)rz A coroutine wrapped in a Future.TNFr%r>context eager_startcFt|||jr |jd=tj|sd|_t d||dt|_nt||_d|_ d|_ d|_ ||_ |tj|_n||_|r+|j"j%r|j'y|j"j)|j*|j t-|y)Nr$Fza coroutine was expected, got zTask-rrB)super__init___source_tracebackr iscoroutine_log_destroy_pending TypeError_task_name_counter_namestr_num_cancels_requested _must_cancel _fut_waiter_coro contextvars copy_context_context_loop is_running_Task__eager_start call_soon _Task__stepr)selfcoror%r>rBrC __class__s r&rHz Task.__init__os d#  ! !&&r*%%d+).D %>r'cd|_|jry|xjdz c_|j|jj |ryd|_||_y)aRequest that this task cancel itself. This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop. The coroutine then has a chance to clean up or even deny the request using try/except/finally. Unlike Future.cancel, this does not guarantee that the task will be cancelled: the exception might be caught and acted upon, delaying cancellation of the task or preventing cancellation completely. The task may also return a value or raise a different exception. Immediately after this method is called, Task.cancelled() will not return True (unless the task was already cancelled). A task will be marked as cancelled when the wrapped coroutine terminates with a CancelledError exception (even if cancel() was not called). This also increases the task's count of cancellation requests. FrmsgT)_log_tracebackr0rPrRcancelrQ_cancel_message)r\rs r&rz Task.cancelsf,$ 99; ##q(#    '&&3&/ "r'c|jS)zReturn the count of the task's cancellation requests. This count is incremented when .cancel() is called and may be decremented using .uncancel(). rPris r& cancellingzTask.cancellings ***r'cb|jdkDr|xjdzc_|jS)zDecrement the task's count of cancellation requests. This should be called by the party that called `cancel()` on the task beforehand. Returns the remaining number of cancellation requests. rrrris r&uncancelz Task.uncancels/  & & *  ' '1 , '***r'ct|j|} t| |jj |j dt | t|j|}||usJ |jr d|_d}yt|y#t |wxYw#|jr d|_d}wt|wxYw# t|j|}||usJ |jr d|_d}wt|w#|jr d|_d}wt|wxYwxYwrg) _swap_current_taskrW_register_eager_taskrVrun!_Task__step_run_and_handle_result_unregister_eager_taskr0rSr)r\ prev_taskcurtasks r& __eager_startzTask.__eager_starts&tzz48  )  & - !!$"C"CTJ&t, ),TZZC$&99;!%DJD"4('t, 99;!%DJD"4( ),TZZC$&99;!%DJD"4( 99;!%DJD"4(sF C&B CB* B''C*'CED3&E'EEc|jrtjd|d||jr1t |tj s|j }d|_d|_t|j| |j|t|j|d}y#t|j|d}wxYw)Nz_step(): already done: z, F) r0rInvalidStateErrorrQ isinstanceCancelledError_make_cancelled_errorrRrrWrr)r\excs r&__stepz Task.__step#s 99;..)$C7;= =   c:#<#<=002 %D DJJ%   - -c 2  D )D  D )Ds B11C c|j} ||jd}n|j|}t|dd}|lt j ||j urGtd|d|d}|j j|j||jd}y|r||urCtd|}|j j|j||jd}yd|_ |j|j|j||_|jrN|jj!|j"r'd|_ d}ytd |d |}|j j|j||j d}y|4|j j|j|jd}yt%j&|rFtd |d |}|j j|j||jd}ytd |}|j j|j||j d}yd}y#t($rS}|jr"d|_t*|A|j"nt*|Y|j.Yd}~d}yd}~wt0j2$r!}||_t*|AYd}~d}yd}~wt6t8f$r}t*|u|d}~wt<$r}t*|u|Yd}~d}yd}~wwxYw#d}wxYw) N_asyncio_future_blockingzTask z got Future z attached to a different looprFzTask cannot await on itself: Frz-yield was used instead of yield from in task z with z;yield was used instead of yield from for generator in task zTask got bad yield: )rSsendthrowgetattrrr/rWr,rZr[rVradd_done_callback _Task__wakeuprRrQrrinspect isgenerator StopIterationrGrwrsrr_cancelled_excKeyboardInterrupt SystemExitrz BaseException)r\rr]rvblockingnew_excr^s r&__step_run_and_handle_resultz!Task.__step_run_and_handle_result4sezzG {4C$v'A4HH#$$V,DJJ>*x|!*$ACDGJJ(( Wdmm)EPDM~".;D8D#F ,, KK$---IDD?;@700 MM4==1B+1(,,#//66(,(<(< 7 >49 10D-+##'(& <=GJJ(( Wdmm)E&D! $$T[[$--$HD$$V,&))-vjBC $$KK$--%AD ')=fZ'HI $$KK$--%AD4DA .  $)!4#7#78"399-tDs(( "%D  GN  lDk":.  G !# &  ' G !# & &bDe 'dDs%JA5M,AM5A0M)AM03M&AMAM MAKMM5L MM#L33 M?MMMMM!c |j|jd}y#t$r}|j|Yd}~d}yd}~wwxYwrg)rvr[r)r\futurers r&__wakeupz Task.__wakeupsH  MMO KKM  KK   s% A AA rg)__name__ __module__ __qualname____doc__rKrHre classmethodr__class_getitem__rjrlrorqr8rwrzr~rrrrrYr[rr __classcell__r^s@r&rrSs+. %)d"!> $L1+ IL"&7.$(d ?(T+ +)&"IVr'rr>rBctj}||j|}n|j||}t|||S)z]Schedule the execution of a coroutine object in a spawn task. Return a Task object. rF)rr!rr?)r]r>rBr%r=s r&rrsK  " " $D%g64 Kr')timeout return_whencKtj|stj|r!t dt |j |s td|tttfvrtd|t|}td|Dr t dtj}t||||d{S7w)a}Wait for the Futures or Tasks given by fs to complete. The fs iterable must not be empty. Coroutines will be wrapped in Tasks. Returns two sets of Future: (done, pending). Usage: done, pending = await asyncio.wait(fs) Note: This does not raise TimeoutError! Futures that aren't done when the timeout occurs are returned in the second set. zexpect a list of futures, not zSet of Tasks/Futures is empty.zInvalid return_when value: c3FK|]}tj|ywrg)rrJ).0fs r& zwait..s 1: ! !! $ 1s!z6Passing coroutines is forbidden, use tasks explicitly.N)risfuturerrJrLtyper ValueErrorrrrsetanyrr!_wait)fsrrr%s r&rrs z55b98b9J9J8KLMM 9::?O]KK6{mDEE RB 1b 11PQQ  " " $Dr7K6 66 6sCC C CcH|js|jdyyrg)r0rw)waiterargss r&_release_waiterrs ;;=$ r'cK|T|dkrOt|}|jr|jSt|d{ |jStj|4d{|d{cdddd{S7N#tj $r }t |d}~wwxYw7C7;7-#1d{7swYyxYww)aWait for the single Future or coroutine to complete, with timeout. Coroutine will be wrapped in Task. Returns result of the Future or coroutine. When a timeout occurs, it cancels the task and raises TimeoutError. To avoid the task cancellation, wrap it in shield(). If the wait is cancelled, the task is also cancelled. If the task suppresses the cancellation and returns a value instead, that value is returned. This function is a coroutine. Nr) r r0rv_cancel_and_waitrr TimeoutErrorrr)futrrs r&rrsFw!|C  88:::< s### (::< (y $(( (C ' (sACBC BC2B63C6B<<B8=B< C B: CB3'B..B33C8B<:C<CC C CcB K|sJd|j d ||j|t  t| fd}|D]}|j | d{  j |D]}|j | tt}}|D]5}|jr|j|%|j|7||fS7#  j |D]}|j |wxYww)zVInternal helper for wait(). The fs argument must be a collection of Futures. zSet of Futures is empty.Ncdzdks2tk(s)tk(rW|jsF|j5j j sj dyyyyy)Nrr)rr cancelledryrr0rw)rcounterrtimeout_handlers r&_on_completionz_wait.._on_completionst1  qL ? * ? *AKKM01 0I)%%';;=!!$'!1J5B *r') create_future call_laterrlenrrremove_done_callbackrr0add) rrrr%rrr0pendingrrrs ` @@@r&rr s( )))2    !FN/6J"gG (, N+,3  %  ! ! # 3A " "> 2 3E35'D  668 HHQK KKN  =   %  ! ! # 3A " "> 2 3s1A D'C0,C.-C01A=D.C00,DDc2Ktj}|j}tjt |}|j | |j|d{|j|y7#|j|wxYww)z._on_timeoutds7 "A " "> 2 OOD ! " r'c|syj|j|sjyyyrg)removerr)rr0rrs r&rz$as_completed.._on_completionjs;  A 2  ! ! #3tr'cKjd{}|tj|jS7&wrg)r#rrrv)rr0s r& _wait_for_onez#as_completed.._wait_for_oners7((*  9)) )xxz sA>'A)rrrrJrLrrqueuesrrget_event_looprr rrranger) rrrr%rrr_rr0rrs @@@@r&r r Hs$z55b9=d2h>O>O=PQRR 7D  "D14R 9AM!$ ' 9DN $, N+, #+> 3t9 o9 :sA:DC=A.Dc#Kdyw)zSkip one event loop run cycle. This is a private helper for 'asyncio.sleep()', used when the 'delay' is set to 0. It uses a bare 'yield' expression (which Task.__step knows how to handle) instead of creating a Future object. Nrr'r&__sleep0rs  sc0K|dkrtd{|Stj}|j}|j |t j ||} |d{|jS7g7#|jwxYww)z9Coroutine that completes after a given time (in seconds).rN)rrr!rrr_set_result_unless_cancelledr)delayrvr%rhs r&r r s zj  " " $D    !F << (A|     s:BA=A B#B(A?)B,B?BBBr$ctj|r&|"|tj|ur td|Sd}t j |s.t j|rd}||}d}n td|tj} |j|S#t$r|r|jwxYw)zmWrap a coroutine or an awaitable in a future. If the argument is a Future, it is returned directly. zRThe future belongs to a different loop than the one specified as the loop argumentTc"K|d{S7wrgr) awaitables r&_wrap_awaitablez&ensure_future.._wrap_awaitables&&s  Fz:An asyncio.Future, a coroutine or an awaitable is required)rrr/rrrJr isawaitablerLrrrr,close)coro_or_futurer% should_closers r&r r s '  G,=,=n,M MEF FL  ! !. 1   ~ . '-^._done_callbacks1Q =EJJL==?   }}//1##C(mmo?'',  G $==?%33!119++-C--/C{!jjls# $"&&//1##C(  ); r'rNr$Fr) rrrrwr rr/rKr0r rr) r coros_or_futuresr%r arg_to_fut done_futsargrrrrrs ` @@@@r&r r s*< $$&""$  5*5*nJH EII D E j $/C|((-#~ ,1( QJE!JsOxxz  %%%n5S/C/2 XD 1E s Lr'ct|jrStj}|j fdfd}j j |S)aWait for a future, shielding it from cancellation. The statement task = asyncio.create_task(something()) res = await shield(task) is exactly equivalent to the statement res = await something() *except* that if the coroutine containing it is cancelled, the task running in something() is not cancelled. From the POV of something(), the cancellation did not happen. But its caller is still cancelled, so the yield-from expression still raises CancelledError. Note: If something() is cancelled by other means this will still cancel shield(). If you want to completely ignore cancellation (not recommended) you can combine shield() with a try/except clause, as follows: task = asyncio.create_task(something()) try: res = await shield(task) except CancelledError: res = None Save a reference to tasks passed to this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn't referenced elsewhere may get garbage collected at any time, even before it's done. c0jr!|js|jy|jrjy|j}|j|yj |j yrg)rryrrzrwrv)innerrrs r&_inner_done_callbackz$shield.._inner_done_callbacksj ?? ??$!  ??  LLN//#C##C(  0r'cJjsjyyrg)r0r)rrrs r&_outer_done_callbackz$shield.._outer_done_callbacks zz|  & &'; <r')r r0rr/rr)rr%rrrrs @@@r&r r askB # E zz|   U #D    E1"= 01 01 Lr'ctjs tdtjj fd}j |S)zsSubmit a coroutine object to a given event loop. Return a concurrent.futures.Future to access the result. zA coroutine object is requiredc tjty#ttf$rt $r'}j rj|d}~wwxYw)Nr$)r _chain_futurer rrrset_running_or_notify_cancelrz)rr]rr%s r&callbackz*run_coroutine_threadsafe..callbacks]   ! !-4"@& I-.   224$$S)  s!%A$"AA$)rrJrL concurrentrFuturecall_soon_threadsafe)r]r%r"rs`` @r&rrsM  ! !$ '899    & & (F h' Mr'cdddfd }|S)a=Create a function suitable for use as a task factory on an event-loop. Example usage: loop.set_task_factory( asyncio.create_eager_task_factory(my_task_constructor)) Now, tasks created will be started immediately (rather than being first scheduled to an event loop). The constructor argument can be any callable that returns a Task-compatible object and has a signature compatible with `Task.__init__`; it must have the `eager_start` keyword argument. Most applications will use `Task` for `custom_task_constructor` and in this case there's no need to call `create_eager_task_factory()` directly. Instead the global `eager_task_factory` instance can be used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`. Nrc||||dS)NTrAr)r%r]r>rBcustom_task_constructors r&factoryz*create_eager_task_factory..factorys& t$TK Kr'r)r(r)s` r&rrs&%)$K Nr'c.tj|y)z;Register an asyncio Task scheduled to run on an event loop.N)r+rr=s r&rrsr'c.tj|y)z6Register an asyncio Task about to be eagerly executed.N)r*rr+s r&rrsTr'chtj|}|td|d|d|t|<y)NzCannot enter into task z while another task z is being executed.r"r#r,r%r=rs r&rrsL!%%d+L4TH=##/"22EGH HN4r'chtj|}||urtd|d|dt|=y)Nz Leaving task z! does not match the current task .r.r/s r&rrsJ!%%d+L4]4(3//;.>aAB Btr'cXtj|}| t|=|S|t|<|Srg)r"r#)r%r=rs r&rrs9""4(I | 4   $t r'c.tj|y)z'Unregister a completed, scheduled Task.N)r+discardr+s r&rrsT"r'c.tj|y)z6Unregister a task which finished its first eager step.N)r*r4r+s r&rr sr') rrrrrrrr+r*r"rrg)Pr__all__concurrent.futuresr#rTrrr-typesr:weakrefrr rrrrrrcount__next__rMrrr? _PyFuturer_PyTask_asyncio_CTask ImportErrorrrrrrrrrrr coroutinerr r r$rr r rrrWeakSetr+rr*r"rrrrrrr_py_current_task_py_register_task_py_register_eager_task_py_unregister_task_py_unregister_eager_task_py_enter_task_py_leave_task_py_swap_current_task_c_current_task_c_register_task_c_register_eager_task_c_unregister_task_c_unregister_eager_task _c_enter_task _c_leave_task_c_swap_current_taskrr'r&rSsM6   %Y__Q'00$>6 z7  zz " MM!D6#D $$$44$$44""00 # 7@ 0d)X%$!%6r  "+/@w~~:16CL?D.4/t4 #7??$u    #   ".&2*.((((#O%1)5MM-i  T  s$F5 G5F>=F>G G