o cc@Y @sdZddlZddlZddlZddlZddlZdZdZdZdZ dZ dZ d Z d Z d Ze e e e egZe d e d e de dediZedZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZGd!d"d"eZd#d$Zd%d&Zd5d'd(Z e!d)d*Z"defd+d,Z#d5d-d.Z$Gd/d0d0eZ%Gd1d2d2eZ&Gd3d4d4e'Z(dS)6z"Brian Quinlan (brian@sweetapp.com)NFIRST_COMPLETEDFIRST_EXCEPTION ALL_COMPLETED _AS_COMPLETEDPENDINGRUNNING CANCELLEDCANCELLED_AND_NOTIFIEDFINISHEDpendingrunning cancelledfinishedzconcurrent.futuresc@eZdZdZdS)Errorz-Base class for all future-related exceptions.N__name__ __module__ __qualname____doc__rry/home/lily/lilypond-2.24.1/release/binaries/dependencies/install/Python-3.10.8/lib/python3.10/concurrent/futures/_base.pyr-rc@r)CancelledErrorzThe Future was cancelled.Nrrrrrr1rrc@r) TimeoutErrorz*The operation exceeded the given deadline.Nrrrrrr5rrc@r)InvalidStateErrorz+The operation is not allowed in this state.Nrrrrrr9rrc@s0eZdZdZddZddZddZdd Zd S) _Waiterz;Provides the event that wait() and as_completed() block on.cCst|_g|_dSN) threadingZEventeventfinished_futuresselfrrr__init__?s  z_Waiter.__init__cC|j|dSrr appendr"futurerrr add_resultCz_Waiter.add_resultcCr$rr%r'rrr add_exceptionFr*z_Waiter.add_exceptioncCr$rr%r'rrr add_cancelledIr*z_Waiter.add_cancelledN)rrrrr#r)r+r,rrrrr=s  rcsDeZdZdZfddZfddZfddZfdd ZZS) _AsCompletedWaiterzUsed by as_completed().cstt|t|_dSr)superr-r#rLocklockr! __class__rrr#Osz_AsCompletedWaiter.__init__cF|jtt|||jWddS1swYdSr)r0r.r-r)rsetr'r1rrr)S "z_AsCompletedWaiter.add_resultcr3r)r0r.r-r+rr4r'r1rrr+Xr5z _AsCompletedWaiter.add_exceptioncr3r)r0r.r-r,rr4r'r1rrr,]r5z _AsCompletedWaiter.add_cancelled) rrrrr#r)r+r, __classcell__rrr1rr-Ls    r-cs8eZdZdZfddZfddZfddZZS)_FirstCompletedWaiterz*Used by wait(return_when=FIRST_COMPLETED).ct||jdSr)r.r)rr4r'r1rrr)e z _FirstCompletedWaiter.add_resultcr8r)r.r+rr4r'r1rrr+ir9z#_FirstCompletedWaiter.add_exceptioncr8r)r.r,rr4r'r1rrr,mr9z#_FirstCompletedWaiter.add_cancelled)rrrrr)r+r,r6rrr1rr7bs   r7csLeZdZdZfddZddZfddZfdd Zfd d ZZ S) _AllCompletedWaiterzr'r1rrr)  z_AllCompletedWaiter.add_resultcs,t||jr|jdS|dSr)r.r+r<rr4r>r'r1rrr+s  z!_AllCompletedWaiter.add_exceptioncr?r)r.r,r>r'r1rrr,r@z!_AllCompletedWaiter.add_cancelled) rrrrr#r>r)r+r,r6rrr1rr:qs   r:c@s(eZdZdZddZddZddZdS) _AcquireFutureszDA context manager that does an ordered acquire of Future conditions.cCst|td|_dS)N)key)sortedidfutures)r"rErrrr#sz_AcquireFutures.__init__cCs|jD]}|jqdSr)rE _conditionacquirer'rrr __enter__  z_AcquireFutures.__enter__cGs|jD]}|jqdSr)rErFrelease)r"argsr(rrr__exit__rIz_AcquireFutures.__exit__N)rrrrr#rHrLrrrrrAs  rAcCs|tkrt}n-|tkrt}n%tdd|D}|tkr$t|dd}n|tkr/t|dd}ntd||D]}|j |q7|S)Ncss|] }|jttfvVqdSr_stater r .0frrr s z._create_and_install_waiters..T)r<FzInvalid return condition: %r) rr-rr7sumrr:r ValueError_waitersr&)fs return_whenwaiterZ pending_countrQrrr_create_and_install_waiterss rYccsl|r4|d}|D]}||q |j|j|Wdn1s%wY~|V|sdSdS)a~ Iterate on the list *fs*, yielding finished futures one by one in reverse order. Before yielding a future, *waiter* is removed from its waiters and the future is removed from each set in the collection of sets *ref_collect*. The aim of this function is to avoid keeping stale references after the future is yielded and before the iterator resumes. N)removerFrUpop)rVrX ref_collectrQZ futures_setrrr_yield_finished_futuress    r^c cs|dur |t}t|}t|}t|tdd|D}||}t|t}Wdn1s4wYt|}zt|||fdEdH|r|durRd}n|t}|dkrft dt||f|j ||j |j }g|_ |j Wdn1swY|t||||fdEdH|sKW|D]}|j|j|Wdn1swYqdS|D]}|j|j|Wdn1swYqw)anAn iterator over the given futures that yields each as it completes. Args: fs: The sequence of Futures (possibly created by different Executors) to iterate over. timeout: The maximum number of seconds to wait. If None, then there is no limit on the wait time. Returns: An iterator that yields the given Futures as they complete (finished or cancelled). If any given Futures are duplicated, they will be returned once. Raises: TimeoutError: If the entire result iterator could not be generated before the given timeout. Ncss"|] }|jttfvr|VqdSrrMrOrrrrRszas_completed..)r]rz%d (of %d) futures unfinished)time monotonicr4lenrArYrlistr^rrwaitr0r clearreverserFrUr[) rVtimeoutend_timeZ total_futuresrr rXZ wait_timeoutrQrrr as_completedsd         rhDoneAndNotDoneFuturesz done not_donec Cs4t|}t|Ydd|D}||}|tkr&|r&t||WdS|tkrA|rAtdd|DrAt||WdSt|t|krUt||WdSt||}Wdn1sdwY|j ||D]}|j |j |Wdn1swYqq| |jt|||S)aoWait for the futures in the given sequence to complete. Args: fs: The sequence of Futures (possibly created by different Executors) to wait upon. timeout: The maximum number of seconds to wait. If None, then there is no limit on the wait time. return_when: Indicates when this function should return. The options are: FIRST_COMPLETED - Return when any future finishes or is cancelled. FIRST_EXCEPTION - Return when any future finishes by raising an exception. If no future raises an exception then it is equivalent to ALL_COMPLETED. ALL_COMPLETED - Return when all futures finish or are cancelled. Returns: A named 2-tuple of sets. The first set, named 'done', contains the futures that completed (is finished or cancelled) before the wait completed. The second set, named 'not_done', contains uncompleted futures. Duplicate futures given to *fs* are removed and will be returned only once. cSsh|] }|jttfvr|qSrrMrOrrr $szwait..Ncss(|]}|s|dur|VqdSr)r exceptionrOrrrrR*s zwait..)r4rArriranyrarYrrcrFrUr[updater )rVrfrWdoneZnot_donerXrQrrrrc s0       rccCs*zz ||W|W~S|w~wr)resultcancel)Zfutrfrrr_result_or_cancel<s   rqc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dddZ dddZddZddZddZeejZdS) Futurez5Represents the result of an asynchronous computation.cCs,t|_t|_d|_d|_g|_g|_dS)z8Initializes the future. Should not be called by clients.N) rZ ConditionrFrrN_result _exceptionrU_done_callbacksr!rrrr#Js  zFuture.__init__c Cs:|jD]}z||Wqtytd|YqwdS)N!exception calling callback for %r)ru ExceptionLOGGERrk)r"callbackrrr_invoke_callbacksSs   zFuture._invoke_callbackscCs|jP|jtkr>|jr%d|jjt|t|j|jjjfWdSd|jjt|t|j|jjjfWdSd|jjt|t|jfWdS1sVwYdS)Nz<%s at %#x state=%s raised %s>z <%s at %#x state=%s returned %s>z<%s at %#x state=%s>) rFrNr rtr2rrD_STATE_TO_DESCRIPTION_MAPrsr!rrr__repr__Zs.  $zFuture.__repr__cCs|j0|jttfvr WddS|jttfvr$ WddSt|_|jWdn1s6wY|dS)zCancel the future if possible. Returns True if the future was cancelled, False otherwise. A future cannot be cancelled if it is running or has already completed. NFT)rFrNrr rr notify_allrzr!rrrrpns  z Future.cancelcCs8|j|jttfvWdS1swYdS)z(Return True if the future was cancelled.N)rFrNrr r!rrrr s $zFuture.cancelledcCs4|j |jtkWdS1swYdS)z1Return True if the future is currently executing.N)rFrNrr!rrrr s$zFuture.runningcCs:|j|jtttfvWdS1swYdS)z>Return True if the future was cancelled or finished executing.N)rFrNrr r r!rrrrns$z Future.donecCs|jr z|jd}w|jSr)rtrsr!rrrZ __get_results zFuture.__get_resultcCs|j|jtttfvr|j| WddSWdn1s%wYz||WdStyAt d|YdSw)a%Attaches a callable that will be called when the future finishes. Args: fn: A callable that will be called with this future as its only argument when the future completes or is cancelled. The callable will always be called by a thread in the same process in which it was added. If the future has already completed or been cancelled then the callable will be called immediately. These callables are called in the order that they were added. Nrv) rFrNrr r rur&rwrxrk)r"fnrrradd_done_callbacks   zFuture.add_done_callbackNcCszT|jD|jttfvrt|jtkr"|WdWd}S|j||jttfvr2t|jtkrE|WdWd}St1sKwYWd}dSd}w)aBReturn the result of the call that the future represents. Args: timeout: The number of seconds to wait for the result if the future isn't done. If None, then there is no limit on the wait time. Returns: The result of the call that the future represents. Raises: CancelledError: If the future was cancelled. TimeoutError: If the future didn't finish executing before the given timeout. Exception: If the call raised then that exception will be raised. N) rFrNrr rr _Future__get_resultrcrr"rfrrrros$   z Future.resultcCs|j<|jttfvrt|jtkr|jWdS|j||jttfvr-t|jtkr<|jWdSt1sBwYdS)aUReturn the exception raised by the call that the future represents. Args: timeout: The number of seconds to wait for the exception if the future isn't done. If None, then there is no limit on the wait time. Returns: The exception raised by the call that the future represents or None if the call completed without raising. Raises: CancelledError: If the future was cancelled. TimeoutError: If the future didn't finish executing before the given timeout. N) rFrNrr rr rtrcrrrrrrks    zFuture.exceptioncCs|j<|jtkr t|_|jD]}||q WddS|jtkr1t|_ WddSt dt ||jt d1sBwYdS)aMark the future as running or process any cancel notifications. Should only be used by Executor implementations and unit tests. If the future has been cancelled (cancel() was called and returned True) then any threads waiting on the future completing (though calls to as_completed() or wait()) are notified and False is returned. If the future was not cancelled then it is put in the running state (future calls to running() will return True) and True is returned. This method should be called by Executor implementations before executing the work associated with this future. If this method returns False then the work should not be executed. Returns: False if the Future was cancelled, True otherwise. Raises: RuntimeError: if this method was already called or if set_result() or set_exception() was called. NFTz!Future %s in unexpected state: %szFuture in unexpected state) rFrNrr rUr,rrrxZcriticalrD RuntimeError)r"rXrrrset_running_or_notify_cancels"     z#Future.set_running_or_notify_cancelcC|j/|jttthvrtd|j|||_t|_|jD]}| |q|j Wdn1s5wY| dS)zSets the return value of work associated with the future. Should only be used by Executor implementations and unit tests. {}: {!r}N) rFrNrr r rformatrsrUr)r}rz)r"rorXrrr set_result    zFuture.set_resultcCr)zSets the result of the future as being the given exception. Should only be used by Executor implementations and unit tests. rN) rFrNrr r rrrtrUr+r}rz)r"rkrXrrr set_exception*rzFuture.set_exceptionr)rrrrr#rzr|rpr r rnrrrorkrrr classmethodtypes GenericAlias__class_getitem__rrrrrrGs"   #"(rrc@sHeZdZdZddZdddddZdd d d d ZddZddZdS)ExecutorzCThis is an abstract base class for concrete asynchronous executors.cOst)a Submits a callable to be executed with the given arguments. Schedules the callable to be executed as fn(*args, **kwargs) and returns a Future instance representing the execution of the callable. Returns: A Future representing the given call. )NotImplementedError)r"r~rKkwargsrrrsubmit>s zExecutor.submitNr=)rf chunksizecsBdur tfddt|Dfdd}|S)a}Returns an iterator equivalent to map(fn, iter). Args: fn: A callable that will take as many arguments as there are passed iterables. timeout: The maximum number of seconds to wait. If None, then there is no limit on the wait time. chunksize: The size of the chunks the iterable will be broken into before being passed to a child process. This argument is only used by ProcessPoolExecutor; it is ignored by ThreadPoolExecutor. Returns: An iterator equivalent to: map(func, *iterables) but the calls may be evaluated out-of-order. Raises: TimeoutError: If the entire result iterator could not be generated before the given timeout. Exception: If fn(*args) raises for any values. Ncsg|] }jg|RqSr)r)rPrK)r~r"rr bsz Executor.map..c 3spz,r"durtVn ttVsWD]}|q%dSD]}|q0wr)rerqr\r_r`rp)r()rgrVrfrrresult_iteratorfs  z%Executor.map..result_iterator)r_r`zip)r"r~rfrZ iterablesrr)rgr~rVr"rfrmapIs   z Executor.mapTF)cancel_futurescCsdS)a;Clean-up the resources associated with the Executor. It is safe to call this method several times. Otherwise, no other methods can be called after this one. Args: wait: If True then shutdown will not return until all running futures have finished executing and the resources used by the executor have been reclaimed. cancel_futures: If True then shutdown will cancel all pending futures. Futures that are completed or running will not be cancelled. Nr)r"rcrrrrshutdownuszExecutor.shutdowncCs|Srrr!rrrrHszExecutor.__enter__cCs|jdddS)NT)rcF)r)r"exc_typeZexc_valZexc_tbrrrrLs zExecutor.__exit__)T) rrrrrrrrHrLrrrrr;s , rc@r)BrokenExecutorzR Raised when a executor has become non-functional after a severe failure. Nrrrrrrsrr)) __author__ collectionsZloggingrr_rrrrrrrrr r Z_FUTURE_STATESr{Z getLoggerrxrwrrrrobjectrr-r7r:rArYr^rh namedtuplerircrqrrrrrrrrrsb   > 3 uR