WccsdZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z Gd d ejZ Gd d Zdd dZdZdS))RunnerrunN) coroutines)events) exceptions)tasks) constantscseZdZdZdZdZdS)_StateZcreatedZ initializedZclosedN)__name__ __module__ __qualname__CREATED INITIALIZEDCLOSED(/usr/lib64/python3.12/asyncio/runners.pyr r sGK FFFrr csPeZdZdZddddZdZdZdZdZdd 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_factorycstj|_||_||_d|_d|_d|_d|_dS)NrF) r r _state_debug _loop_factory_loop_context_interrupt_count_set_event_loop)selfrr r__init__zRunner.__init__0s@nn  )  !$rcs(|j|SN) _lazy_initr r __enter__zRunner.__enter__9s  rcs(|jdSr)close)rZexc_typeZexc_valZexc_tb r__exit__zRunner.__exit__=s rcsj|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_completeZshutdown_asyncgensZshutdown_default_executorrZTHREAD_JOIN_TIMEOUTrrset_event_loopr$r)rloops 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+CAD2cs<|j|jS)zReturn embedded event loop.)r rr!r"rget_loopzRunner.get_loopQs zzrcontextcs*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)rZ iscoroutineZ ValueErrorZformatr_get_running_loop RuntimeErrorr rrZ create_task threadingZcurrent_threadZ main_threadsignalZ getsignalZSIGINTZdefault_int_handler functoolsZpartial _on_sigintrr(rZCancelledErrorZgetattrKeyboardInterrupt)rZcoror-taskZsigint_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"JcsB|jtjur td|jtjurdS|j Pt j|_|js?t j|jd|_ n|j |_|j&|jj|jtj|_tj|_dS)NzRunner is closedT)rr rr1rrrZnew_event_looprrr)rZ set_debug contextvarsZ copy_contextrr!r"rr zRunner._lazy_inits ;;&-- ' '12 2 ;;&,, , , F    %..0DJ'''%%djj11'+$$++-DJ ;; " JJ  - -#002 (( rcs|xjdz c_|jdkrA|js0|j|jj ddSt )NrcsdSrrrrrzz#Runner._on_sigint..sDr)rZdonecancelrZcall_soon_threadsafer6)rZsignumZframer.r%rr5zRunner._on_sigintsj "  A % %inn.>.>      JJ + +LL 9 9 F!!r) r r r Z__doc__rr#r&r$r+rr r5rrrrrs6!%4%%%%%(((" $(+I+I+I+I+IZ)))&"""""rrrcstj 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 loopr:)rr0r1rr)ZmainrZrunnerrrrrs8!- FH H e   zz$                 s AAcsTtj|}|sdS|D]}|j|jtj|ddi|D]K}|j r|j '|jd|j |dMdS)NZreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)Zmessage exceptionr7)rZ all_tasksr9r(ZgatherZ cancelledr;Zcall_exception_handler)r*Z to_cancelr7rrr'r's%I 9 ELL)LtLLMM >>    >>  '  ' 'N!^^-))    r)Z__all__r8Zenumr4r2r3ZrrrrrZEnumr rrr'rrrr<s&   TYY I"I"I"I"I"I"I"X" " " " " Jr