\ddZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z Gdd ej Z Gd d Zdd d ZdZdS))RunnerrunN) coroutines)events) exceptions)tasksceZdZdZdZdZdS)_Statecreated initializedclosedN)__name__ __module__ __qualname__CREATED INITIALIZEDCLOSED ..\python\lib\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_factoryc|tj|_||_||_d|_d|_d|_d|_dS)NrF) r r_state_debug _loop_factory_loop_context_interrupt_count_set_event_loop)selfrrs r__init__zRunner.__init__1s>n  )  !$rc.||SN) _lazy_initr#s r __enter__zRunner.__enter__:s  rc.|dSr&)close)r#exc_typeexc_valexc_tbs r__exit__zRunner.__exit__>s rc |jtjurdS |j}t ||||||jrtj d| d|_tj |_dS#|jrtj d| d|_tj |_wxYw)zShutdown and close event loop.N) rr rr_cancel_all_tasksrun_until_completeshutdown_asyncgensshutdown_default_executorr"rset_event_loopr+r)r#loops rr+z Runner.closeAs ;f0 0 0 F (:D d # # #  # #D$;$;$=$= > > >  # #D$B$B$D$D E E E# ,%d+++ JJLLLDJ -DKKK # ,%d+++ JJLLLDJ -DK ' ' ' 's A$CA D c8||jS)zReturn embedded event loop.)r'rr(s rget_loopzRunner.get_loopQs zrcontextctj|s"td|t jt d|||j}|j ||}tj tj urxtjtjtjurNt%j|j|} tjtj|n#t$rd}YnwxYwd}d|_ |j ||Jtjtj|ur+tjtjtjSSS#t.j$r<|jdkr/t3|dd}||dkrt5wxYw#|Jtjtj|ur+tjtjtjwwwxYw)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 loopr9) main_taskruncancel)r iscoroutine ValueErrorformatr_get_running_loop RuntimeErrorr'r r create_task threadingcurrent_thread main_threadsignal getsignalSIGINTdefault_int_handler functoolspartial _on_sigintr!r2rCancelledErrorgetattrKeyboardInterrupt)r#coror:tasksigint_handlerr=s rrz Runner.runVs5%d++ PAHHNNOO O  # % % 1IKK K  ?mGz%%dG%<<  $ & &)*?*A*A A A //63MMM&.t$OOON & fm^<<<< & & &"&  & "N ! I:0066*$V]33~EE fmV-GHHHH+E(   $q(("4T::'HHJJ!OO+---   *$V]33~EE fmV-GHHHH+Es,>D D-,D-:F!!A G,,G//AH>c|jtjurtd|jtjurdS|j@t j|_|j s t j |jd|_ n||_|j |j |j tj|_tj|_dS)NzRunner is closedT)rr rrBrrrnew_event_looprr"r5r set_debug contextvars copy_contextr r(s rr'zRunner._lazy_inits ;&- ' '122 2 ;&, , , F   %.00DJ' ,%dj111'+$++--DJ ; " J  - - -#022 ( rc|xjdz c_|jdkrE|s1||jddSt )NrcdSr&rrrrz#Runner._on_sigint..sDr)r!donecancelrcall_soon_threadsaferP)r#signumframer<s rrMzRunner._on_sigintsn "  A % %inn.>.> %       J + +LL 9 9 9 F!!!r) rrr__doc__r$r)r/r+r8rr'rMrrrrrs6!%4%%%%%(((  $(+I+I+I+I+IZ)))&"""""rrrctjtdt|5}||cdddS#1swxYwYdS)aExecute the coroutine and return the result. This function runs the passed coroutine, taking care of managing the asyncio event loop and finalizing asynchronous generators. 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. Example: async def main(): await asyncio.sleep(1) print('hello') asyncio.run(main()) Nz8asyncio.run() cannot be called from a running event looprb)rrArBrr)mainrrunners rrrs0!!- FHH H e    zz$                  sAAAcbtj|}|sdS|D]}||tj|ddi|D]V}|r|+|d||dWdS)Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message exceptionrR)r all_tasksr]r2gather cancelledricall_exception_handler)r6 to_cancelrRs rr1r1s%%I  EL)LtLLMMM >>     >>   '  ' 'N!^^--))    r)__all__rWenumrKrDrGsysrrrr Enumr rrr1rrrrts&   TY H"H"H"H"H"H"H"H"V     Br