3 \R @sPdZddlZddlZddlZddlZdZdZdZdZdZ dZ d Z d Z d Z e e e e e gZe d e d e de de diZejdZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZd!d"Zd#d$Zd/d%d&Zejd'd(Z defd)d*Z!Gd+d,d,eZ"Gd-d.d.eZ#dS)0z"Brian Quinlan (brian@sweetapp.com)NFIRST_COMPLETEDFIRST_EXCEPTION ALL_COMPLETED _AS_COMPLETEDPENDINGRUNNING CANCELLEDCANCELLED_AND_NOTIFIEDFINISHEDpendingrunning cancelledfinishedzconcurrent.futuresc@s eZdZdS)ErrorN)__name__ __module__ __qualname__rr0/usr/lib64/python3.6/concurrent/futures/_base.pyr,src@s eZdZdS)CancelledErrorN)rrrrrrrr0src@s eZdZdS) TimeoutErrorN)rrrrrrrr4src@s,eZdZddZddZddZddZd S) _WaitercCstj|_g|_dS)N) threadingZEventeventfinished_futures)selfrrr__init__:s z_Waiter.__init__cCs|jj|dS)N)rappend)rfuturerrr add_result>sz_Waiter.add_resultcCs|jj|dS)N)rr)rrrrr add_exceptionAsz_Waiter.add_exceptioncCs|jj|dS)N)rr)rrrrr add_cancelledDsz_Waiter.add_cancelledN)rrrrrr r!rrrrr8srcs@eZdZfddZfddZfddZfddZZS) _AsCompletedWaitercstt|jtj|_dS)N)superr"rrLocklock)r) __class__rrrJsz_AsCompletedWaiter.__init__c s0|j tt|j||jjWdQRXdS)N)r%r#r"rrset)rr)r&rrrNsz_AsCompletedWaiter.add_resultc s0|j tt|j||jjWdQRXdS)N)r%r#r"r rr')rr)r&rrr Ssz _AsCompletedWaiter.add_exceptionc s0|j tt|j||jjWdQRXdS)N)r%r#r"r!rr')rr)r&rrr!Xsz _AsCompletedWaiter.add_cancelled)rrrrrr r! __classcell__rr)r&rr"Gs   r"cs4eZdZfddZfddZfddZZS)_FirstCompletedWaitercstj||jjdS)N)r#rrr')rr)r&rrr`s z _FirstCompletedWaiter.add_resultcstj||jjdS)N)r#r rr')rr)r&rrr ds z#_FirstCompletedWaiter.add_exceptioncstj||jjdS)N)r#r!rr')rr)r&rrr!hs z#_FirstCompletedWaiter.add_cancelled)rrrrr r!r(rr)r&rr)]s  r)csHeZdZfddZddZfddZfddZfd d ZZS) _AllCompletedWaitercs$||_||_tj|_tjdS)N)num_pending_callsstop_on_exceptionrr$r%r#r)rr+r,)r&rrros z_AllCompletedWaiter.__init__c Cs4|j$|jd8_|js&|jjWdQRXdS)N)r%r+rr')rrrr_decrement_pending_callsusz,_AllCompletedWaiter._decrement_pending_callscstj||jdS)N)r#rr.)rr)r&rrr{s z_AllCompletedWaiter.add_resultcs*tj||jr|jjn|jdS)N)r#r r,rr'r.)rr)r&rrr s  z!_AllCompletedWaiter.add_exceptioncstj||jdS)N)r#r!r.)rr)r&rrr!s z!_AllCompletedWaiter.add_cancelled) rrrrr.rr r!r(rr)r&rr*ls    r*c@s$eZdZddZddZddZdS)_AcquireFuturescCst|td|_dS)N)key)sortedidfutures)rr3rrrrsz_AcquireFutures.__init__cCsx|jD]}|jjqWdS)N)r3 _conditionacquire)rrrrr __enter__s z_AcquireFutures.__enter__cGsx|jD]}|jjqWdS)N)r3r4release)rargsrrrr__exit__s z_AcquireFutures.__exit__N)rrrrr6r9rrrrr/sr/cCs|tkrt}nZ|tkr t}nJtdd|D}|tkrHt|dd}n"|tkr^t|dd}n td|x|D]}|j j |qpW|S)Ncss|]}|jttgkVqdS)N)_stater r ).0frrr sz._create_and_install_waiters..T)r,FzInvalid return condition: %r) rr"rr)sumrr*r ValueError_waitersr)fs return_whenwaiterZ pending_countr<rrr_create_and_install_waiterss  rDc csXxR|rR|d}x|D]}|j|qW|j|jj|WdQRX~|jVqWdS)Nr-)remover4r@pop)rArC ref_collectr<Z futures_setrrr_yield_finished_futuress  rIc csJ|dk r|tj}t|}t|}t|*tdd|D}||}t|t}WdQRXt|}zt|||fdEdHx|r|dkrd}n(|tj}|dkrt dt||f|j j ||j |j }g|_ |j jWdQRX|jt||||fdEdHq~WWdx,|D]$}|j|jj|WdQRXqWXdS)Ncss |]}|jttgkr|VqdS)N)r:r r )r;r<rrrr=szas_completed..)rHrz%d (of %d) futures unfinished)time monotonicr'lenr/rDrlistrIrrwaitr%rclearreverser4r@rF) rAtimeoutend_timeZ total_futuresrr rCZ wait_timeoutr<rrr as_completeds@     rSDoneAndNotDoneFuturesz done not_donecCst|tdd|D}t||}|tkr>|r>t||S|tkrf|rftdd|Drft||St|t|krt||St||}WdQRX|jj |x*|D]"}|j |j j |WdQRXqW|j |jt|t||S)Ncss |]}|jttgkr|VqdS)N)r:r r )r;r<rrrr=szwait..css(|] }|j r|jdk r|VqdS)N)r exception)r;r<rrrr=$s)r/r'rrTranyrLrDrrNr4r@rFupdater)rArQrBdoneZnot_donerCr<rrrrNs"          rNc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dddZ dddZ ddZddZddZdS) FuturecCs,tj|_t|_d|_d|_g|_g|_dS)N) rZ Conditionr4rr:_result _exceptionr@_done_callbacks)rrrrr8s  zFuture.__init__c CsBx<|jD]2}y ||Wqtk r8tjd|YqXqWdS)Nz!exception calling callback for %r)r\ ExceptionLOGGERrU)rcallbackrrr_invoke_callbacksAs   zFuture._invoke_callbacksc Cs|jv|jtkr`|jrz <%s at %#x state=%s returned %s>z<%s at %#x state=%s>) r4r:r r[r&rr2_STATE_TO_DESCRIPTION_MAPrZ)rrrr__repr__Hs" zFuture.__repr__c CsR|j:|jttgkrdS|jttgkr,dSt|_|jjWdQRX|jdS)NFT)r4r:rr rr notify_allr`)rrrrcancel\sz Future.cancelc Cs |j|jttgkSQRXdS)N)r4r:rr )rrrrr oszFuture.cancelledc Cs|j |jtkSQRXdS)N)r4r:r)rrrrr tszFuture.runningc Cs"|j|jtttgkSQRXdS)N)r4r:rr r )rrrrrXysz Future.donecCs|jr|jn|jSdS)N)r[rZ)rrrrZ __get_result~szFuture.__get_resultc Cs>|j&|jtttgkr(|jj|dSWdQRX||dS)N)r4r:rr r r\r)rfnrrradd_done_callbacks  zFuture.add_done_callbackNc Csx|jh|jttgkrtn|jtkr0|jS|jj||jttgkrRtn|jtkrd|jStWdQRXdS)N) r4r:rr rr _Future__get_resultrNr)rrQrrrresults   z Future.resultc Cst|jd|jttgkrtn|jtkr.|jS|jj||jttgkrPtn|jtkr`|jStWdQRXdS)N) r4r:rr rr r[rNr)rrQrrrrUs   zFuture.exceptionc Cst|jd|jtkr6t|_x|jD]}|j|q WdS|jtkrJt|_dStj dt ||jt dWdQRXdS)NFTz!Future %s in unexpected state: %szFuture in unexpected state) r4r:rr r@r!rrr^Zcriticalr2 RuntimeError)rrCrrrset_running_or_notify_cancels   z#Future.set_running_or_notify_cancelc CsN|j6||_t|_x|jD]}|j|qW|jjWdQRX|jdS)N)r4rZr r:r@rrcr`)rrhrCrrr set_results zFuture.set_resultc CsN|j6||_t|_x|jD]}|j|qW|jjWdQRX|jdS)N)r4r[r r:r@r rcr`)rrUrCrrr set_exception s zFuture.set_exception)N)N)rrrrr`rbrdr r rXrgrfrhrUrjrkrlrrrrrY5s   "( rYc@s>eZdZddZdddddZdd d Zd d Zd dZdS)ExecutorcOs tdS)N)NotImplementedError)rrer8kwargsrrrsubmits zExecutor.submitNr-)rQ chunksizecsBdk rtjfddt|Dfdd}|S)Ncsg|]}jf|qSr)rp)r;r8)rerrr ?sz Executor.map..c 3sdzFjx8rBdkr(jjVq jjtjVq WWdxD] }|jqNWXdS)N)rPrGrhrJrKrd)r)rRrArQrrresult_iteratorCs  z%Executor.map..result_iterator)rJrKzip)rrerQrq iterablesrsr)rRrerArrQrmap&s   z Executor.mapTcCsdS)Nr)rrNrrrshutdownRs zExecutor.shutdowncCs|S)Nr)rrrrr6_szExecutor.__enter__cCs|jdddS)NT)rNF)rw)rexc_typeZexc_valZexc_tbrrrr9bs zExecutor.__exit__)T)rrrrprvrwr6r9rrrrrms  , rm)N)$ __author__ collectionsZloggingrrJrrrrrrrr r Z_FUTURE_STATESraZ getLoggerr^r]rrrobjectrr"r)r*r/rDrIrS namedtuplerTrNrYrmrrrrsR  >1d