WcdZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z Gd d ejZ Gd d Zdd dZdZdS))RunnerrunN) coroutines)events) exceptions)tasks) constantsceZdZdZdZdZdS)_Statecreated initializedclosedN)__name__ __module__ __qualname__CREATED INITIALIZEDCLOSED(/usr/lib64/python3.12/asyncio/runners.pyr r sGK FFFrr cPeZdZdZddddZdZdZdZdZdd d Z d Z d Z dS) ra5A context manager that controls event loop life cycle. The context manager always creates a new event loop, allows to run async functions inside it, and properly finalizes the loop at the context manager exit. If debug is True, the event loop will be run in debug mode. If loop_factory is passed, it is used for new event loop creation. asyncio.run(main(), debug=True) is a shortcut for with asyncio.Runner(debug=True) as runner: runner.run(main()) The run() method can be called multiple times within the runner's context. This can be useful for interactive console (e.g. IPython), unittest runners, console tools, -- everywhere when async code is called from existing sync framework and where the preferred single asyncio.run() call doesn't work. N)debug loop_factoryctj|_||_||_d|_d|_d|_d|_dS)NrF) r r_state_debug _loop_factory_loop_context_interrupt_count_set_event_loop)selfrrs r__init__zRunner.__init__0s@nn  )  !$rc(|j|SN) _lazy_initr$s r __enter__zRunner.__enter__9s  rc(|jdSr')close)r$exc_typeexc_valexc_tbs r__exit__zRunner.__exit__=s rcj|jtjurdS |j}t ||j |j |j |jtj|jrtjd|jd|_tj|_dS#|jrtjdjd|_tj|_wxYw)zShutdown and close event loop.N)rr rr _cancel_all_tasksrun_until_completeshutdown_asyncgensshutdown_default_executorr THREAD_JOIN_TIMEOUTr#rset_event_loopr,r)r$loops rr,z Runner.close@s ;;f00 0 0 F (::D d # #  # #D$;$;$= > >  # #..y/L/LM O O###%%d++ JJLLDJ --DKKK ###%%d++ JJLLDJ --DK ' ' ' 's A+CAD2c<|j|jS)zReturn embedded event loop.)r(r r)s rget_loopzRunner.get_loopQs zzrcontextc*tj|stdj|t j t d|j| |j}|jj||}tjtjurtjtj tj"urLt%j&|j(|} tjtj |nd}d|_ |jj-||`tjtj |ur7tjtj tj"SSS#t$rd}YwxYw#t.j0$r;|j*dkr)t3|dd}||dkr t5wxYw#|^tjtj |ur6tjtj tj"wxYw)z/Run a coroutine inside the embedded event loop.z"a coroutine was expected, got {!r}Nz7Runner.run() cannot be called from a running event loopr;) main_taskruncancel)r iscoroutine ValueErrorformatr_get_running_loop RuntimeErrorr(r!r create_task threadingcurrent_thread main_threadsignal getsignalSIGINTdefault_int_handler functoolspartial _on_sigintr"r3rCancelledErrorgetattrKeyboardInterrupt)r$coror<tasksigint_handlerr?s rrz Runner.runVsD%%d++AHHNO O  # # % 1IK K  ?mmGzz%%dG%<  $ $ &)*?*?*A A A  /63M3MMM&..t$OON & fmm^<<<"N ! I::006*$$V]]3~EE fmmV-G-GHHHF+% & & &"&  &((   $$q(("4T:'HHJ!OO+--   *$$V]]3~EE fmmV-G-GHH I I I Is,'G G GGAH--H00A"JcB|jtjur td|jtjurdS|j Pt j|_|js?t j|jd|_ n|j |_|j&|jj|jtj|_tj|_dS)NzRunner is closedT)rr rrDrrrnew_event_loopr r#r7r set_debug contextvars copy_contextr!r)s rr(zRunner._lazy_inits ;;&-- ' '12 2 ;;&,, , , F    %..0DJ'''%%djj11'+$$++-DJ ;; " JJ  - -#002 (( rc|xjdz c_|jdkrA|js0|j|jj ddSt )NrcdSr'rrrrz#Runner._on_sigint..sDr)r"donecancelr call_soon_threadsaferR)r$signumframer>s rrOzRunner._on_sigintsj "  A % %inn.>.>      JJ + +LL 9 9 F!!r) rrr__doc__r%r*r0r,r:rr(rOrrrrrs6!%4%%%%%(((" $(+I+I+I+I+IZ)))&"""""rrrctj tdt|5}|j |cdddS#1swYdSxYw)azExecute the coroutine and return the result. This function runs the passed coroutine, taking care of managing the asyncio event loop, finalizing asynchronous generators and closing the default executor. This function cannot be called when another asyncio event loop is running in the same thread. If debug is True, the event loop will be run in debug mode. This function always creates a new event loop and closes it at the end. It should be used as a main entry point for asyncio programs, and should ideally only be called once. The executor is given a timeout duration of 5 minutes to shutdown. If the executor hasn't finished within that duration, a warning is emitted and the executor is closed. Example: async def main(): await asyncio.sleep(1) print('hello') asyncio.run(main()) Nz8asyncio.run() cannot be called from a running event looprd)rrCrDrr)mainrrunners rrrs8!- FH H e   zz$                 s AAcTtj|}|sdS|D]}|j|jtj|ddi|D]K}|j r|j '|jd|j |dMdS)Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message exceptionrT)r all_tasksr_r3gather cancelledrkcall_exception_handler)r8 to_cancelrTs rr2r2s%I 9 ELL)LtLLMM >>    >>  '  ' 'N!^^-))    r)__all__rYenumrMrFrIrrrr r Enumr rrr2rrrrus&   TYY I"I"I"I"I"I"I"X" " " " " Jr