iXc@sddlZddlZddlZddlZddlZddlZdZdZdZdZ dZ dZ dZ d Z d Zd Ze e e eegZid e 6d e 6de 6de6de6ZejdZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefd YZd!Zed"Z ej!d#d$Z"ee d%Z#d&efd'YZ$d(efd)YZ%dS(*iNs"Brian Quinlan (brian@sweetapp.com)tFIRST_COMPLETEDtFIRST_EXCEPTIONt ALL_COMPLETEDt _AS_COMPLETEDtPENDINGtRUNNINGt CANCELLEDtCANCELLED_AND_NOTIFIEDtFINISHEDtpendingtrunningt cancelledtfinishedsconcurrent.futurestErrorcBseZdZRS(s-Base class for all future-related exceptions.(t__name__t __module__t__doc__(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR .stCancelledErrorcBseZdZRS(sThe Future was cancelled.(RRR(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR2st TimeoutErrorcBseZdZRS(s*The operation exceeded the given deadline.(RRR(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR6st_WaitercBs2eZdZdZdZdZdZRS(s;Provides the event that wait() and as_completed() block on.cCstj|_g|_dS(N(t threadingtEventteventtfinished_futures(tself((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt__init__<scCs|jj|dS(N(Rtappend(Rtfuture((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt add_result@scCs|jj|dS(N(RR(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt add_exceptionCscCs|jj|dS(N(RR(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt add_cancelledFs(RRRRRRR(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR:s    t_AsCompletedWaitercBs2eZdZdZdZdZdZRS(sUsed by as_completed().cCs&tt|jtj|_dS(N(tsuperRRRtLocktlock(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRLscCs7|j(tt|j||jjWdQXdS(N(R"R RRRtset(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRPs cCs7|j(tt|j||jjWdQXdS(N(R"R RRRR#(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRUs cCs7|j(tt|j||jjWdQXdS(N(R"R RRRR#(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRZs (RRRRRRR(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRIs    t_FirstCompletedWaitercBs)eZdZdZdZdZRS(s*Used by wait(return_when=FIRST_COMPLETED).cCs'tt|j||jjdS(N(R R$RRR#(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRbscCs'tt|j||jjdS(N(R R$RRR#(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRfscCs'tt|j||jjdS(N(R R$RRR#(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRjs(RRRRRR(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR$_s  t_AllCompletedWaitercBs;eZdZdZdZdZdZdZRS(s<Used by wait(return_when=FIRST_EXCEPTION and ALL_COMPLETED).cCs8||_||_tj|_tt|jdS(N(tnum_pending_callststop_on_exceptionRR!R"R R%R(RR&R'((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRqs  cCs<|j-|jd8_|js2|jjnWdQXdS(Ni(R"R&RR#(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt_decrement_pending_callsws  cCs$tt|j||jdS(N(R R%RR((RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR}scCs=tt|j||jr/|jjn |jdS(N(R R%RR'RR#R((RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRs cCs$tt|j||jdS(N(R R%RR((RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRs(RRRRR(RRR(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR%ns     t_AcquireFuturescBs)eZdZdZdZdZRS(sDA context manager that does an ordered acquire of Future conditions.cCst|dt|_dS(Ntkey(tsortedtidtfutures(RR-((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRscCs%x|jD]}|jjq WdS(N(R-t _conditiontacquire(RR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt __enter__scGs%x|jD]}|jjq WdS(N(R-R.trelease(RtargsR((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt__exit__s(RRRRR0R3(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR)s  cCs|tkrt}n|tkr0t}nhtd|D}|tkrgt|dt}n1|tkrt|dt }nt d|x|D]}|j j |qW|S(Ncss$|]}|jttgkVqdS(N(t_stateRR(t.0tf((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pys sR'sInvalid return condition: %r( RRRR$tsumRR%tTrueRtFalset ValueErrort_waitersR(tfst return_whentwaitert pending_countR6((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt_create_and_install_waiterss       c cs|dk r|tj}nt|}t|4td|D}||}t|t}WdQXzx|D] }|VqwWx|rJ|dkrd}nA|tj}|dkrtdt|t|fn|jj ||j $|j }g|_ |jj WdQXx |D]}|V|j |q+WqWWdx.|D]&}|j|jj |WdQXqVWXdS(snAn 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. css*|] }|jttgkr|VqdS(N(R4RR(R5R6((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pys sNis%d (of %d) futures unfinished(tNonettimeR#R)R@RRtlenRtwaitR"RtcleartremoveR.R;( R<ttimeouttend_timeR R R>Rt wait_timeoutR6((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt as_completeds<                tDoneAndNotDoneFuturess done not_donec Cs,t|td|D}t||}|tkrR|rRt||S|tkr|rtd|Drt||Snt|t|krt||St||}WdQX|jj |x.|D]&}|j |j j |WdQXqW|j |jt|t||S(sWait 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. css*|] }|jttgkr|VqdS(N(R4RR(R5R6((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pys scss4|]*}|j r|jdk r|VqdS(N(R t exceptionRA(R5R6((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pys sN(R)R#RRKRtanyRCR@RRDR.R;RFtupdateR(R<RGR=tdonetnot_doneR>R6((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRDs"     tFuturecBseZdZdZdZdZdZdZdZdZ dZ d Z dd Z dd Zdd Zd ZdZdZdZRS(s5Represents the result of an asynchronous computation.cCsItj|_t|_d|_d|_d|_g|_ g|_ dS(s8Initializes the future. Should not be called by clients.N( Rt ConditionR.RR4RAt_resultt _exceptiont _tracebackR;t_done_callbacks(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR s     cCssxl|jD]a}y||Wq tk rAtjd|q tk rTq tjd|q Xq WdS(Ns!exception calling callback for %r(RVt ExceptiontLOGGERRLt BaseException(Rtcallback((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt_invoke_callbacks*s  cCs|j|jtkr|jrOdtt|t|j|jjjfSdtt|t|j|j jjfSndtt|t|jfSWdQXdS(Ns!s#s( R.R4RRTthexR,t_STATE_TO_DESCRIPTION_MAPt __class__RRS(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt__repr__As    cCsf|jM|jttgkr#tS|jttgkr<tSt|_|jjWdQX|j tS(sCancel 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. N( R.R4RRR9RRR8t notify_allR[(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytcancelRs   cCs'|j|jttgkSWdQXdS(s(Return True if the future has cancelled.N(R.R4RR(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR es cCs!|j|jtkSWdQXdS(s1Return True if the future is currently executing.N(R.R4R(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR js cCs*|j|jtttgkSWdQXdS(s>Return True of the future was cancelled or finished executing.N(R.R4RRR(R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyROos cCs\|jrQt|jtjr-|jj}nt|j}||j|jn|jSdS(N(RTt isinstancettypest InstanceTypeR^ttypeRURS(Rtexception_type((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt __get_resultts  cCsJ|j1|jtttgkr6|jj|dSWdQX||dS(s%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. N(R.R4RRRRVR(Rtfn((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytadd_done_callbacks  cCs|j|jttgkr+tn|jtkrD|jS|jj||jttgkrutn"|jtkr|jStWdQXdS(sBReturn 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( R.R4RRRRt_Future__get_resultRDR(RRG((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytresults     cCs|j|jttgkr+tn|jtkrJ|j|jfS|jj||jttgkr{tn(|jtkr|j|jfSt WdQXdS(sqReturn a tuple of (exception, traceback) 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( R.R4RRRRRTRURDR(RRG((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytexception_infos   cCs|j|dS(sUReturn 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. i(Rl(RRG((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRLscCs|j|jtkrGt|_x|jD]}|j|q,WtS|jtkrct|_t St j dt ||jt dWdQXdS(sMark 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. s!Future %s in unexpected state: %ssFuture in unexpected stateN(R.R4RRR;RR9RRR8RXtcriticalR,t RuntimeError(RR>((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytset_running_or_notify_cancels      cCs^|jE||_t|_x|jD]}|j|q&W|jjWdQX|jdS(sSets the return value of work associated with the future. Should only be used by Executor implementations and unit tests. N(R.RSRR4R;RR`R[(RRkR>((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt set_results   cCsg|jN||_||_t|_x|jD]}|j|q/W|jjWdQX|jdS(sSets the result of the future as being the given exception and traceback. Should only be used by Executor implementations and unit tests. N( R.RTRURR4R;RR`R[(RRLt tracebackR>((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytset_exception_infos    cCs|j|ddS(sSets the result of the future as being the given exception. Should only be used by Executor implementations and unit tests. N(RrRA(RRL((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyt set_exception*sN(RRRRR[R_RaR R RORjRiRARkRlRLRoRpRrRs(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRQs"         !  ( tExecutorcBs>eZdZdZdZedZdZdZRS(sCThis is an abstract base class for concrete asynchronous executors.cOs tdS(s 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. N(tNotImplementedError(RRhR2tkwargs((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytsubmit4s csx|jddk r.tjngtj|D]}|j||^q>fd}|S(s}Returns a 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. 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. RGc3sjzGx@D]8}dkr*|jVq |jtjVq WWdxD]}|jqQWXdS(N(RARkRBRa(R(RHR<RG(s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytresult_iteratorYs    N(tgetRARBt itertoolstizipRw(RRht iterablesRvR2Rx((RHR<RGs</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytmap?s  . cCsdS(sClean-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. N((RRD((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytshutdownes cCs|S(N((R((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR0rscCs|jdttS(NRD(R~R8R9(Rtexc_typetexc_valtexc_tb((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyR3us( RRRRwR}R8R~R0R3(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pyRt1s  & (&t collectionstloggingRRzRBRct __author__RRRRRRRRRt_FUTURE_STATESR]t getLoggerRXRWR RRtobjectRRR$R%R)R@RARJt namedtupleRKRDRQRt(((s</usr/lib/python2.7/site-packages/concurrent/futures/_base.pytsV          ; 1