fdZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z Gd d ejZ Gd d Zddd dZdZy))RunnerrunN) coroutines)events) exceptions)tasks) constantsceZdZdZdZdZy)_Statecreated initializedclosedN)__name__ __module__ __qualname__CREATED INITIALIZEDCLOSED&/usr/lib/python3.12/asyncio/runners.pyr r sGK Frr cNeZdZdZddddZdZdZdZdZdd d Z d Z d Z y) 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. Ndebug loop_factoryctj|_||_||_d|_d|_d|_d|_y)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$|jyr()close)r%exc_typeexc_valexc_tbs r__exit__zRunner.__exit__=s  rcF|jtjury |j}t ||j |j |j |jtj|jrtjd|jd|_tj|_y#|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  (::D d #  # #D$;$;$= >  # #..y/L/LM O##%%d+ JJLDJ --DK ##%%d+ JJLDJ --DKs A$CAD c:|j|jS)zReturn embedded event loop.)r)r!r*s rget_loopzRunner.get_loopQs zzrcontextctj|stdj|t j t d|j| |j}|jj||}tjtjurztjtj tj"urGt%j&|j(|} tjtj |nd}d|_ |jj-||Ytjtj |ur3tjtj tj"SSS#t$rd}YwxYw#t.j0$r4|j*dkDr#t3|dd}||dk(r t5wxYw#|Ytjtj |ur3tjtj tj"wwwxYw)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#r4rCancelledErrorgetattrKeyboardInterrupt)r%coror=tasksigint_handlerr@s rrz Runner.runVs%%d+AHHNO O  # # % 1IK K  ?mmGzz%%dG%<  $ $ &)*?*?*A A  /63M3MM&..t$ON & fmm^<"N ! I::006*$$V]]3~E fmmV-G-GHF+% &"&  &(( $$q("4T:'HJ!O+--   *$$V]]3~E fmmV-G-GHF+s,$F,6F=, F:9F:=AHHAI$c$|jtjur td|jtjury|j Lt j|_|jsz#Runner._on_sigint..sr)r#donecancelr!call_soon_threadsaferS)r%signumframer?s rrPzRunner._on_sigintsT "  A %inn.>     JJ + +L 9 !!r) rrr__doc__r&r+r1r-r;rr)rPrrrrrs=6!%4%(" $(+IZ)&"rrrctj tdt||5}|j |cdddS#1swYyxYw)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)rrDrErr)mainrrrunners rrrsT8!- FH H e, 7 6zz$   s AAcBtj|}|sy|D]}|j|jtj|ddi|D]G}|j r|j %|jd|j |dIy)Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message exceptionrU)r all_tasksr`r4gather cancelledrkcall_exception_handler)r9 to_cancelrUs rr3r3s%I   ELL)LtLM >>   >>  '  ' 'N!^^-)  r)__all__rZenumrNrGrJrrrr r Enumr rrr3rrrrusW   TYY I"I"X$" Jr