a d*@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZdZe jdkredddZGdddejZGdddej Z!Gdddej"ej#Z$Gdddej%Z&GdddZ'Gddde'Z(d d!Z)Gd"d#d#e'Z*Gd$d%d%e*Z+Gd&d'd'e*Z,Gd(d)d)e'Z-Gd*d+d+e'Z.Gd,d-d-ej/Z0eZ1e0Z2dS).z2Selector event loop for Unix with signal handling.N) base_events)base_subprocess) constants) coroutines)events) exceptions)futures)selector_events)tasks) transports)logger)SelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherPidfdChildWatcherMultiLoopChildWatcherThreadedChildWatcherDefaultEventLoopPolicyZwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N)signumframerr8/opt/alt/python39/lib64/python3.9/asyncio/unix_events.py_sighandler_noop*srcseZdZdZd)fdd ZfddZddZd d Zd d Zd dZ ddZ d*ddZ d+ddZ d,ddZ ddZd-dddddddZd.dddddddd Zd!d"Zd#d$Zd%d&Zd'd(ZZS)/_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncst|i|_dSN)super__init___signal_handlers)selfselector __class__rrr5s z_UnixSelectorEventLoop.__init__csZtts.t|jD]}||qn(|jrVtjd|dt |d|j dS)NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removalsource) rclosesys is_finalizinglistrremove_signal_handlerwarningswarnResourceWarningclearr sigr"rrr&9s z_UnixSelectorEventLoop.closecCs|D]}|sq||qdSr)_handle_signal)r datarrrr_process_self_dataGsz)_UnixSelectorEventLoop._process_self_datac GsRt|st|rtd|||zt|j Wn4t t fyv}zt t |WYd}~n d}~00t|||d}||j|<zt|tt|dWnt yL}z|j|=|jsztdWn6t t fy}ztd|WYd}~n d}~00|jtjkr6t d|dnWYd}~n d}~00dS)zAdd a handler for a signal. UNIX only. Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. z3coroutines cannot be used with add_signal_handler()NFset_wakeup_fd(-1) failed: %ssig  cannot be caught)rZ iscoroutineZiscoroutinefunction TypeError _check_signalZ _check_closedsignal set_wakeup_fdZ_csockfileno ValueErrorOSError RuntimeErrorstrrZHandlerr siginterruptr infoerrnoEINVAL)r r0callbackargsexchandleZnexcrrradd_signal_handlerNs2  "  "z)_UnixSelectorEventLoop.add_signal_handlercCs8|j|}|durdS|jr*||n ||dS)z2Internal helper that is the actual signal handler.N)rgetZ _cancelledr*Z_add_callback_signalsafe)r r0rHrrrr1{s   z%_UnixSelectorEventLoop._handle_signalc Cs||z |j|=Wnty*YdS0|tjkr>tj}ntj}zt||WnDty}z,|jtj krt d|dnWYd}~n d}~00|jszt dWn4t tfy}zt d|WYd}~n d}~00dS)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. Fr6r7Nr4r5T)r9rKeyErrorr:SIGINTdefault_int_handlerSIG_DFLr>rCrDr?r;r=r rB)r r0handlerrGrrrr*s(     "z,_UnixSelectorEventLoop.remove_signal_handlercCs6t|tstd||tvr2td|dS)zInternal helper to validate a signal. Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. zsig must be an int, not zinvalid signal number N) isinstanceintr8r: valid_signalsr=r/rrrr9s  z$_UnixSelectorEventLoop._check_signalcCst|||||Sr)_UnixReadPipeTransportr pipeprotocolwaiterextrarrr_make_read_pipe_transportsz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||Sr)_UnixWritePipeTransportrTrrr_make_write_pipe_transportsz1_UnixSelectorEventLoop._make_write_pipe_transportc st} | std|} t||||||||f| |d| } | | |j| z| IdHWn@t t fyYn*t y| | IdHYn0Wdn1s0Y| S)NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rWrX)rget_child_watcher is_activer? create_future_UnixSubprocessTransportadd_child_handlerZget_pid_child_watcher_callback SystemExitKeyboardInterrupt BaseExceptionr&Z_wait) r rVrFshellstdinstdoutstderrbufsizerXkwargswatcherrWtransprrr_make_subprocess_transports.    &z1_UnixSelectorEventLoop._make_subprocess_transportcCs||j|dSr)call_soon_threadsafeZ_process_exited)r pid returncoderlrrrrasz._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamessl_handshake_timeoutc s |dust|tsJ|r,|durLtdn |durr errorZbindr&rCZ EADDRINUSEr~rr}rZServerZ_start_servingr sleep) r rrrrrrqrtrerrrGmsgZserverrrrcreate_unix_serversh            z)_UnixSelectorEventLoop.create_unix_serverc sz tjWnty&tdYn0z |}Wn4ttjfyh}ztdWYd}~n d}~00zt|j }Wnt ytdYn0|r|n|}|sdS| } | | d|||||d| IdHS)Nzos.sendfile() is not availableznot a regular filer) rxsendfileAttributeErrorrSendfileNotAvailableErrorr<ioUnsupportedOperationfstatst_sizer>r^_sock_sendfile_native_impl) r rrfileoffsetcountr<rZfsize blocksizefutrrr_sock_sendfile_nativeJs,        z,_UnixSelectorEventLoop._sock_sendfile_nativec Cs,|} |dur|||r4||||dS|rd||}|dkrd||||||dSzt| |||} WnDttfy|dur| ||| | |j || |||||| Yndt yj} z|dur| j t jkrt| turtdt j} | | _| } |dkr>td} |||||| n|||||| WYd} ~ nd} ~ 0ttfyYnty} z$|||||| WYd} ~ nrd} ~ 00| dkr||||||nD|| 7}|| 7}|dur | ||| | |j || |||||| dS)Nrzsocket is not connectedzos.sendfile call failed)r< remove_writer cancelled_sock_sendfile_update_fileposZ set_resultrxrBlockingIOErrorInterruptedError_sock_add_cancellation_callbackZ add_writerrr>rCZENOTCONNrConnectionError __cause__rrZ set_exceptionrbrcrd)r rZ registered_fdrrr<rrr total_sentfdZsentrGnew_excrrrrrasp                z1_UnixSelectorEventLoop._sock_sendfile_native_implcCs|dkrt||tjdSNr)rxlseekSEEK_SET)r r<rrrrrrsz4_UnixSelectorEventLoop._sock_sendfile_update_fileposcsfdd}||dS)Ncs&|r"}|dkr"|dS)Nr4)rr<r)rrr rrrrcbszB_UnixSelectorEventLoop._sock_add_cancellation_callback..cb)Zadd_done_callback)r rrrrrrrrsz6_UnixSelectorEventLoop._sock_add_cancellation_callback)N)NN)NN)N)N)N)__name__ __module__ __qualname____doc__rr&r3rIr1r*r9rYr[rmrarrrrrr __classcell__rrr"rr/sB -       . CFrcseZdZdZdfdd ZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e jfddZdddZddZddZZS) rSiNcst|||jd<||_||_||_||_d|_d|_ t |jj }t |st |st |sd|_d|_d|_tdt |jd|j|jj||j|jj|j|j|dur|jtj|ddS)NrUFz)Pipe transport is for pipes/sockets only.)rr_extra_loop_piper<_fileno _protocol_closing_pausedrxrrrS_ISFIFOrS_ISCHRr= set_blocking call_soonconnection_made _add_reader _read_readyr _set_result_unless_cancelled)r looprUrVrWrXmoder"rrrs6      z_UnixReadPipeTransport.__init__cCs|jjg}|jdur |dn|jr0|d|d|jt|jdd}|jdur|durt ||jt j }|r|dq|dn |jdur|dn |dd d |S) Nclosedclosingfd= _selectorpollingidleopen<{}> )r#rrappendrrgetattrrr _test_selector_event selectorsZ EVENT_READformatjoin)r rBr!rrrr__repr__s$          z_UnixReadPipeTransport.__repr__c Cszt|j|j}WnDttfy*YntyX}z||dWYd}~nfd}~00|rl|j |nJ|j rt d|d|_|j |j|j |jj|j |jddS)Nz"Fatal read error on pipe transport%r was closed by peerT)rxreadrmax_sizerrr> _fatal_errorrZ data_receivedr get_debugr rBr_remove_readerrZ eof_received_call_connection_lost)r r2rGrrrrs"  z"_UnixReadPipeTransport._read_readycCs>|js |jrdSd|_|j|j|jr:td|dS)NTz%r pauses reading)rrrrrrr debugr rrr pause_readings   z$_UnixReadPipeTransport.pause_readingcCsB|js |jsdSd|_|j|j|j|jr>td|dS)NFz%r resumes reading) rrrrrrrr rrrrrresume_readings   z%_UnixReadPipeTransport.resume_readingcCs ||_dSrrr rVrrr set_protocol sz#_UnixReadPipeTransport.set_protocolcCs|jSrrrrrr get_protocolsz#_UnixReadPipeTransport.get_protocolcCs|jSrrrrrr is_closingsz!_UnixReadPipeTransport.is_closingcCs|js|ddSr)r_closerrrrr&sz_UnixReadPipeTransport.closecCs,|jdur(|d|t|d|jdSNzunclosed transport r$rr-r&r _warnrrr__del__s z_UnixReadPipeTransport.__del__Fatal error on pipe transportcCsZt|tr4|jtjkr4|jrLtjd||ddn|j||||j d| |dSNz%r: %sTexc_info)message exceptionrrV) rPr>rCZEIOrrr rcall_exception_handlerrrr rGrrrrrs z#_UnixReadPipeTransport._fatal_errorcCs(d|_|j|j|j|j|dSNT)rrrrrrr rGrrrr-sz_UnixReadPipeTransport._closecCsPz,|j|W|jd|_d|_d|_n|jd|_d|_d|_0dSrrZconnection_lostrr&rrrrrr2s  z,_UnixReadPipeTransport._call_connection_lost)NN)r)rrrrrrrrrrrrr&r+r,rrrrrrrr"rrSs rScseZdZd%fdd ZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZejfddZddZd&dd Zd'd!d"Zd#d$ZZS)(rZNc st||||jd<||_||_||_t|_d|_ d|_ t |jj }t|}t|}t|} |s|s| sd|_d|_d|_tdt |jd|j|jj|| s|rtjds|j|jj|j|j|dur|jtj|ddS)NrUrFz?Pipe transport is only for pipes, sockets and character devicesZaix)rrrrr<rr bytearray_buffer _conn_lostrrxrrrrrrr=rrrrr'platform startswithrrr r) r rrUrVrWrXrZis_charZis_fifoZ is_socketr"rrr?s6        z _UnixWritePipeTransport.__init__cCs|jjg}|jdur |dn|jr0|d|d|jt|jdd}|jdur|durt ||jt j }|r|dn |d| }|d|n |jdur|dn |dd d |S) Nrrrrrrzbufsize=rrr)r#rrrrrrrr rrZ EVENT_WRITEget_write_buffer_sizerr)r rBr!rrirrrrds(          z _UnixWritePipeTransport.__repr__cCs t|jSr)lenrrrrrr|sz-_UnixWritePipeTransport.get_write_buffer_sizecCs6|jrtd||jr*|tn|dS)Nr)rrr rBrrBrokenPipeErrorrrrrrs   z#_UnixWritePipeTransport._read_readyc CsLt|tttfsJt|t|tr.t|}|s6dS|jsB|jrj|jtjkrXt d|jd7_dS|j s2zt |j|}Wnnttfyd}YnVttfyYn@ty}z(|jd7_||dWYd}~dSd}~00|t|krdS|dkr t||d}|j|j|j|j |7_ |dS)Nz=pipe closed by peer or os.write(pipe, data) raised exception.rr#Fatal write error on pipe transport)rPbytesr memoryviewreprrrrZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESr warningrrxwriterrrrbrcrdrrrZ _add_writer _write_readyZ_maybe_pause_protocol)r r2nrGrrrrs8      z_UnixWritePipeTransport.writec Cs|jsJdzt|j|j}Wnttfy8YnttfyNYnty}z>|j |j d7_ |j |j| |dWYd}~npd}~00|t|jkr|j |j |j||jr|j |j|ddS|dkr |jd|=dS)NzData should not be emptyrrr)rrxrrrrrbrcrdr.rr_remove_writerrrZ_maybe_resume_protocolrrr)r rrGrrrrs, "   z$_UnixWritePipeTransport._write_readycCsdSrrrrrr can_write_eofsz%_UnixWritePipeTransport.can_write_eofcCsB|jr dS|jsJd|_|js>|j|j|j|jddSr)rrrrrrrrrrrr write_eofs z!_UnixWritePipeTransport.write_eofcCs ||_dSrrrrrrrsz$_UnixWritePipeTransport.set_protocolcCs|jSrrrrrrrsz$_UnixWritePipeTransport.get_protocolcCs|jSrrrrrrrsz"_UnixWritePipeTransport.is_closingcCs|jdur|js|dSr)rrr rrrrr&sz_UnixWritePipeTransport.closecCs,|jdur(|d|t|d|jdSrrrrrrrs z_UnixWritePipeTransport.__del__cCs|ddSr)rrrrrabortsz_UnixWritePipeTransport.abortrcCsNt|tr(|jr@tjd||ddn|j||||jd||dSr) rPr>rrr rrrrrrrrrs  z$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr|j|j|j|j|j|j|j|dSr) rrrr rr.rrrrrrrrs  z_UnixWritePipeTransport._closecCsPz,|j|W|jd|_d|_d|_n|jd|_d|_d|_0dSrrrrrrrs  z-_UnixWritePipeTransport._call_connection_lost)NN)r)N)rrrrrrrrrr r rrrr&r+r,rr rrrrrrr"rrZ<s"% #   rZc@seZdZddZdS)r_c Ksd}|tjkrt\}}zhtj|f||||d|d||_|durh|t|d|d|j_ d}W|dur||n|dur||0dS)NF)rerfrgrhZuniversal_newlinesriwb) buffering) subprocessPIPErzZ socketpairPopen_procr&rdetachrf) r rFrerfrgrhrirjZstdin_wrrr_start s,   z_UnixSubprocessTransport._startN)rrrrrrrrr_ sr_c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)raHAbstract base class for monitoring child processes. Objects derived from this class monitor a collection of subprocesses and report their termination or interruption by a signal. New callbacks are registered with .add_child_handler(). Starting a new process must be done within a 'with' block to allow the watcher to suspend its activity until the new process if fully registered (this is needed to prevent a race condition in some implementations). Example: with watcher: proc = subprocess.Popen("sleep 1") watcher.add_child_handler(proc.pid, callback) Notes: Implementations of this class must be thread-safe. Since child watcher objects may catch the SIGCHLD signal and call waitpid(-1), there should be only one active object per process. cGs tdS)aRegister a new child handler. Arrange for callback(pid, returncode, *args) to be called when process 'pid' terminates. Specifying another callback for the same process replaces the previous handler. Note: callback() must be thread-safe. NNotImplementedErrorr rorErFrrrr`9s z&AbstractChildWatcher.add_child_handlercCs tdS)zRemoves the handler for process 'pid'. The function returns True if the handler was successfully removed, False if there was nothing to remove.Nrr rorrrremove_child_handlerDsz)AbstractChildWatcher.remove_child_handlercCs tdS)zAttach the watcher to an event loop. If the watcher was previously attached to an event loop, then it is first detached before attaching to the new loop. Note: loop may be None. Nrr rrrr attach_loopLsz AbstractChildWatcher.attach_loopcCs tdS)zlClose the watcher. This must be called to make sure that any underlying resource is freed. Nrrrrrr&VszAbstractChildWatcher.closecCs tdS)zReturn ``True`` if the watcher is active and is used by the event loop. Return True if the watcher is installed and ready to handle process exit notifications. Nrrrrrr]]szAbstractChildWatcher.is_activecCs tdS)zdEnter the watcher's context and allow starting new processes This function must return selfNrrrrr __enter__fszAbstractChildWatcher.__enter__cCs tdS)zExit the watcher's contextNrr abcrrr__exit__lszAbstractChildWatcher.__exit__N) rrrrr`rrr&r]rr!rrrrr"s   rc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)ra6Child watcher implementation using Linux's pid file descriptors. This child watcher polls process file descriptors (pidfds) to await child process termination. In some respects, PidfdChildWatcher is a "Goldilocks" child watcher implementation. It doesn't require signals or threads, doesn't interfere with any processes launched outside the event loop, and scales linearly with the number of subprocesses launched by the event loop. The main disadvantage is that pidfds are specific to Linux, and only work on recent (5.3+) kernels. cCsd|_i|_dSrr _callbacksrrrrr}szPidfdChildWatcher.__init__cCs|SrrrrrrrszPidfdChildWatcher.__enter__cCsdSrr)r exc_type exc_value exc_tracebackrrrr!szPidfdChildWatcher.__exit__cCs|jduo|jSrrZ is_runningrrrrr]szPidfdChildWatcher.is_activecCs|ddSrrrrrrr&szPidfdChildWatcher.closecCsd|jdur$|dur$|jr$tdt|jD] \}}}|j|t|q.|j ||_dSNzCA loop is being detached from a child watcher with pending handlers) rr#r+r,RuntimeWarningvaluesrrxr&r.)r rpidfd_rrrrs   zPidfdChildWatcher.attach_loopcGsZ|j|}|dur*|d||f|j|<n,t|}|j||j||||f|j|<dSr)r#rJrxZ pidfd_openrr_do_wait)r rorErFZexistingr,rrrr`s   z#PidfdChildWatcher.add_child_handlercCs~|j|\}}}|j|zt|d\}}Wn"tyTd}td|Yn 0t |}t ||||g|RdS)NrzJchild process pid %d exit status already read: will report returncode 255) r#poprrrxwaitpidChildProcessErrorr r_compute_returncoder&)r ror,rErFr-statusrprrrr.s    zPidfdChildWatcher._do_waitcCsFz|j|\}}}Wnty*YdS0|j|t|dS)NFT)r#r0rKrrrxr&)r ror,r-rrrrs   z&PidfdChildWatcher.remove_child_handlerN) rrrrrrr!r]r&rr`r.rrrrrrqs   rcCs2t|rt| St|r*t|S|SdSr)rx WIFSIGNALEDWTERMSIG WIFEXITED WEXITSTATUS)r4rrrr3s     r3c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)BaseChildWatchercCsd|_i|_dSrr"rrrrrszBaseChildWatcher.__init__cCs|ddSrr(rrrrr&szBaseChildWatcher.closecCs|jduo|jSrr'rrrrr]szBaseChildWatcher.is_activecCs tdSrr)r expected_pidrrr _do_waitpidszBaseChildWatcher._do_waitpidcCs tdSrrrrrr_do_waitpid_allsz BaseChildWatcher._do_waitpid_allcCs~|dust|tjsJ|jdur<|dur<|jrs zBaseChildWatcher._sig_chldN) rrrrr&r]r;r<rr>rrrrr9sr9csPeZdZdZfddZddZddZdd Zd d Zd d Z ddZ Z S)rad'Safe' child watcher implementation. This implementation avoids disrupting other code spawning processes by polling explicitly each process in the SIGCHLD handler instead of calling os.waitpid(-1). This is a safe solution but it has a significant overhead when handling a big number of children (O(n) each time SIGCHLD is raised) cs|jtdSr)r#r.rr&rr"rrr& s zSafeChildWatcher.closecCs|SrrrrrrrszSafeChildWatcher.__enter__cCsdSrrrrrrr!szSafeChildWatcher.__exit__cGs||f|j|<||dSr)r#r;rrrrr`sz"SafeChildWatcher.add_child_handlercCs(z|j|=WdSty"YdS0dSNTFr#rKrrrrrs  z%SafeChildWatcher.remove_child_handlercCst|jD]}||q dSrr)r#r;rrrrr<$sz SafeChildWatcher._do_waitpid_allcCs|dks Jzt|tj\}}Wn&tyH|}d}td|Yn.0|dkrVdSt|}|jrvt d||z|j |\}}Wn,t y|jrtjd|ddYn0|||g|RdS)Nrr/8Unknown child process pid %d, will report returncode 255$process %s exited with returncode %s'Child watcher got an unexpected pid: %rTr) rxr1WNOHANGr2r rr3rrrr#r0rK)r r:ror4rprErFrrrr;)s2       zSafeChildWatcher._do_waitpid) rrrrr&rr!r`rr<r;rrrr"rrs rcsTeZdZdZfddZfddZddZdd Zd d Zd d Z ddZ Z S)raW'Fast' child watcher implementation. This implementation reaps every terminated processes by calling os.waitpid(-1) directly, possibly breaking other code spawning processes and waiting for their termination. There is no noticeable overhead when handling a big number of children (O(1) each time a child terminates). cs$tt|_i|_d|_dSr)rr threadingZLock_lock_zombies_forksrr"rrrVs  zFastChildWatcher.__init__cs"|j|jtdSr)r#r.rIrr&rr"rrr&\s  zFastChildWatcher.closecCs<|j"|jd7_|WdS1s.0YdS)Nr)rHrJrrrrraszFastChildWatcher.__enter__cCst|jN|jd8_|js"|js2WddSt|j}|jWdn1sZ0Ytd|dS)Nrz5Caught subprocesses termination from unknown pids: %s)rHrJrIr@r.r r)r rrr Zcollateral_victimsrrrr!gs  (zFastChildWatcher.__exit__c Gs|jsJd|jPz|j|}Wn.tyT||f|j|<YWddS0Wdn1sj0Y|||g|RdS)NzMust use the context manager)rJrHrIr0rKr#)r rorErFrprrrr`us 2z"FastChildWatcher.add_child_handlercCs(z|j|=WdSty"YdS0dSr@rArrrrrs  z%FastChildWatcher.remove_child_handlerc Csztdtj\}}Wnty*YdS0|dkr8dSt|}|jz|j|\}}WnNty|j r||j |<|j rt d||YWdqd}Yn0|j rt d||Wdn1s0Y|durt d||q|||g|RqdS)Nr4rz,unknown process %s exited with returncode %srDz8Caught subprocess termination from unknown pid: %d -> %d)rxr1rFr2r3rHr#r0rKrJrIrrr rr)r ror4rprErFrrrr<s:      "z FastChildWatcher._do_waitpid_all) rrrrrr&rr!r`rr<rrrr"rrLs  rc@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)ra~A watcher that doesn't require running loop in the main thread. This implementation registers a SIGCHLD signal handler on instantiation (which may conflict with other code that install own handler for this signal). The solution is safe but it has a significant overhead when handling a big number of processes (*O(n)* each time a SIGCHLD is received). cCsi|_d|_dSr)r#_saved_sighandlerrrrrrszMultiLoopChildWatcher.__init__cCs |jduSr)rKrrrrr]szMultiLoopChildWatcher.is_activecCsT|j|jdurdSttj}||jkr:tdnttj|jd|_dS)Nz+SIGCHLD handler was changed by outside code) r#r.rKr: getsignalr=r>r r)r rOrrrr&s     zMultiLoopChildWatcher.closecCs|SrrrrrrrszMultiLoopChildWatcher.__enter__cCsdSrrr r$Zexc_valZexc_tbrrrr!szMultiLoopChildWatcher.__exit__cGs&t}|||f|j|<||dSr)rget_running_loopr#r;)r rorErFrrrrr`sz'MultiLoopChildWatcher.add_child_handlercCs(z|j|=WdSty"YdS0dSr@rArrrrrs  z*MultiLoopChildWatcher.remove_child_handlercCsN|jdurdSttj|j|_|jdurr rrNrArrrrrs   z!MultiLoopChildWatcher.attach_loopcCst|jD]}||q dSrrBrrrrr<sz%MultiLoopChildWatcher._do_waitpid_allc Cs|dks Jzt|tj\}}Wn*tyL|}d}td|d}Yn0|dkrZdSt|}d}z|j|\}}}Wn"t ytjd|ddYnJ0| rtd||n0|r| rt d |||j |||g|RdS) Nrr/rCFTrEr%Loop %r that handles pid %r is closedrD)rxr1rFr2r rr3r#r0rK is_closedrrrn) r r:ror4rpZ debug_logrrErFrrrr;s8      z!MultiLoopChildWatcher._do_waitpidc CsHz |Wn6ttfy$Yn tyBtjdddYn0dS)Nr?Tr)r<rbrcrdr r)r rrrrrr>)s   zMultiLoopChildWatcher._sig_chldN)rrrrrr]r&rr!r`rrr<r;r>rrrrrs %rc@sneZdZdZddZddZddZdd Zd d Zd d Z e j fddZ ddZ ddZddZddZdS)raAThreaded child watcher implementation. The watcher uses a thread per process for waiting for the process finish. It doesn't require subscription on POSIX signal but a thread creation is not free. The watcher has O(1) complexity, its performance doesn't depend on amount of spawn processes. cCstd|_i|_dSr) itertoolsr _pid_counter_threadsrrrrr?s zThreadedChildWatcher.__init__cCsdSrrrrrrr]CszThreadedChildWatcher.is_activecCs |dSr) _join_threadsrrrrr&FszThreadedChildWatcher.closecCs.ddt|jD}|D] }|qdS)z%Internal: Join all non-daemon threadscSsg|]}|r|js|qSr)is_alivedaemon.0threadrrr Ksz6ThreadedChildWatcher._join_threads..N)r)rSr+r)r threadsrYrrrrTIsz"ThreadedChildWatcher._join_threadscCs|SrrrrrrrPszThreadedChildWatcher.__enter__cCsdSrrrMrrrr!SszThreadedChildWatcher.__exit__cCs6ddt|jD}|r2||jdt|ddS)NcSsg|]}|r|qSr)rUrWrrrrZWsz0ThreadedChildWatcher.__del__..z0 has registered but not finished child processesr$)r)rSr+r#r-)r rr[rrrrVs  zThreadedChildWatcher.__del__cGsFt}tj|jdt|j||||fdd}||j|<|dS)Nzwaitpid-T)targetnamerFrV) rrNrGZThreadr;nextrRrSstart)r rorErFrrYrrrr`^s  z&ThreadedChildWatcher.add_child_handlercCsdSrrrrrrrgsz)ThreadedChildWatcher.remove_child_handlercCsdSrrrrrrrmsz ThreadedChildWatcher.attach_loopcCs|dks Jzt|d\}}Wn&tyF|}d}td|Yn 0t|}|rftd|||r~td||n|j |||g|R|j |dS)Nrr/rCrDrO) rxr1r2r rr3rrrPrnrSr0)r rr:rErFror4rprrrr;ps&   z ThreadedChildWatcher._do_waitpidN)rrrrrr]r&rTrr!r+r,rr`rrr;rrrrr2s  rcsHeZdZdZeZfddZddZfddZdd Z d d Z Z S) _UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstd|_dSr)rr_watcherrr"rrrs z$_UnixDefaultEventLoopPolicy.__init__cCs\tjB|jdur:t|_ttur:|j|jj Wdn1sN0YdSr) rrHrarrGcurrent_thread main_threadr_localrrrrr _init_watchers  z)_UnixDefaultEventLoopPolicy._init_watchercs6t||jdur2ttur2|j|dS)zSet the event loop. As a side effect, if a child watcher was set before, then calling .set_event_loop() from the main thread will call .attach_loop(loop) on the child watcher. N)rset_event_looprarGrbrcrrr"rrrfs   z*_UnixDefaultEventLoopPolicy.set_event_loopcCs|jdur||jS)z~Get the watcher for child processes. If not yet set, a ThreadedChildWatcher object is automatically created. N)rarerrrrr\s z-_UnixDefaultEventLoopPolicy.get_child_watchercCs4|dust|tsJ|jdur*|j||_dS)z$Set the watcher for child processes.N)rPrrar&)r rkrrrset_child_watchers  z-_UnixDefaultEventLoopPolicy.set_child_watcher) rrrrrZ _loop_factoryrrerfr\rgrrrr"rr`s   r`)3rrCrrQrxrr:rzrrr'rGr+rrrrrrr r r r logr __all__r ImportErrorrZBaseSelectorEventLooprZ ReadTransportrSZ_FlowControlMixinZWriteTransportrZZBaseSubprocessTransportr_rrr3r9rrrrZBaseDefaultEventLoopPolicyr`rrrrrrsb             NON5Ji}Y2