!A?hdZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z Gdd ej Z Gd d Zdd d ZdZdS))RunnerrunN) coroutines)events) exceptions)tasksceZdZdZdZdZdS)_Statecreated initializedclosedN)__name__ __module__ __qualname__CREATED INITIALIZEDCLOSEDn  )  !$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