ϪfdZddlZddlZddlZddlmZmZmZmZm Z m Z m Z m Z m Z mZmZddlmZddlmZddlmZddlmZmZmZddlmZdd lmZmZmZdd l m!Z!m"Z"dd l#m$Z$dd l%m&Z&e d Z'GddZ(Gdde)Z*Gdde*Z+Gdde*Z,Gdde,Z-Gdde,Z.Gdde,Z/Gdde*Z0GddZ1d Z2d!egdfd"efd#Z3e d$Z4Gd%d&Z5Gd'd(Z6e6Z7d)ee'd"eee'fd*Z8d)ee'd"e5fd+Z9eeGd,d-Z: d:d.ed/e;d!e ed0e'fd1egd9Z?y).counters**$$&C}}!%("$Q''))HNN.>=>.>>## 1@?@1 -H++H5L++C0L </Eqy%("$U++r1)object)clsr=rIr.s ` @r/ withCountzLoopingCall.withCountfs:  67| r1tc|jJ|jJ||jz }t||jz }|S)aD Determine the number of intervals passed as of the given point in time. @param t: The specified time (from the start of the L{LoopingCall}) to be measured in intervals @return: The C{int} number of intervals which have passed as of the given point in time. )r"r!int)r.rM elapsedTime intervalNums r/rCzLoopingCall._intervalOfsK~~)))}}((($..( + 56 r1rDc"|jrJd|dkr tdd|_tx}|_|jj |_||_||_|r ||S|j|j |S)a Start running function every interval seconds. @param interval: The number of seconds between calls. May be less than one. Precision will depend on the underlying platform, the available hardware, and the load on the system. @param now: If True, run this call right now. Otherwise, wait until the interval has elapsed before beginning. @return: A Deferred whose callback will be invoked with C{self} when C{self.stop} is called, or whose errback will be invoked when the function raises an exception or returned a deferred that has its errback invoked. z.Tried to start an already running LoopingCall.rzinterval must be >= 0T) running ValueErrorrr r-rAr"r!rB _scheduleFrom)r.r!rDr<s r/startzLoopingCall.starts <<T!TT a<45 5 %-J.4>++-    F   t~~ .r1c|jsJdd|_|jK|jjd|_|jdc}|_|J|j |yy)zStop running function.z1Tried to stop a LoopingCall that was not running.FN)rSrcancelr callbackr.ds r/stopzLoopingCall.stopsi||SSS| 99 II   DI $ At~= = JJt  !r1c|jsJd|j\|jjd|_|jj |_|j |j yy)zT Skip the next iteration and reset the timer. @since: 11.1 z2Tried to reset a LoopingCall that was not running.N)rSrrXr-rAr"rUr.s r/resetzLoopingCall.resetsb ||TTT| 99 II   DI!ZZ//1DN   t~~ . !r1cdtddffd }dtddffd }d_tjgj ij }|j||j|y)Nresultr'cjr*jjjyjdc}_|J|j yN)rSrUr-rAr rY)rar[r.s r/cbz LoopingCall.__call__..cbsM||""4::#5#5#78$(NND!4>}$} 4 r1failurecfd_jdc}_|J|j|y)NF)rSr errback)rer[r.s r/ebz LoopingCall.__call__..ebs2 DL $ At~= = IIg r1) rJrrrr$r%r& addCallback addErrback)r.rdrhr[s` r/__call__zLoopingCall.__call__sj !v !$ !  D   $&& 5466 5TWW 5 b Rr1whencndtffd }jj|_y)z Schedule the next iteration of this looping call. @param when: The present time from whence the call is scheduled. r'cjdk(ryjJjz }jJj|jzz }|zk(r jS|S)Nr)r!r") runningForuntilNextIntervalr.rls r/howLongz*LoopingCall._scheduleFrom..howLongs}}!>>- --.J==, ,, $ dmm1K L t///}}$% $r1N)floatr- callLaterr)r.rlrqs`` r/rUzLoopingCall._scheduleFroms+ % %:JJ((D9 r1ct|jdd}|9t|jdd}| t|jdd}||d|}|tj|j}dj |j |tj|j tj|jS)N __qualname____name__im_class.z LoopingCall<{!r}>({}, *{}, **{}))getattrr$r safe_reprformatr!r%r&)r.funcimClasss r/__repr__zLoopingCall.__repr__%stvv~t4 <466:t4D!$&&*d;&%Yav.D <$$TVV,D188 MM    dff %   dgg &   r1)Tr'N)!rv __module__ru__doc__rr r__annotations__rSr rr!rrrBr"r#rrJr0propertyr< classmethodrOrLrCboolrVr\r_rkrUstrr~r1r/rr*s?6$(D(< 'G37Ix/07 $Hhuo$K!%Ix%%)M8E?)1(3;/1V161d1 (8M#:;  9huf}&=9-99vUs"e$(=:Q@  /($:%$:D$:L # r1rceZdZdZy)SchedulerErrorz The operation could not be completed because the scheduler or one of its tasks was in an invalid state. This exception should not be raised directly, but is a superclass of various scheduler-state-related exceptions. Nrvrrurrr1r/rr:sr1rceZdZdZy)SchedulerStoppedzt The operation could not complete because the scheduler was stopped in progress or was already stopped. Nrrr1r/rrCr1rceZdZdZy) TaskFinishedz The operation could not complete because the task was already completed, stopped, encountered an error or otherwise permanently stopped running. Nrrr1r/rrJrr1rceZdZdZy)TaskDonezR The operation could not complete because the task was already completed. Nrrr1r/rrQr1rceZdZdZy) TaskStoppedzH The operation could not complete because the task was stopped. Nrrr1r/rrWrr1rceZdZdZy) TaskFailedz] The operation could not complete because the task died with an unhandled error. Nrrr1r/rr]rr1rceZdZdZy) NotPausedz^ This exception is raised when a task is resumed which was not previously paused. Nrrr1r/rrdrr1rc$eZdZdZddZdefdZy)_Timerg{Gz?r'NcPtj|jz|_yrc)time MAX_SLICEendr^s r/r0z_Timer.__init__ns99;/r1cDtj|jk\Src)rrr^s r/rkz_Timer.__call__qsyy{dhh&&r1r)rvrrurr0rrkrr1r/rrksI0'$'r1rg:0yE>callabler'cVddlm}tt|j t |Sr))r,r+r rrs_EPSILON)rr+s r/_defaultSchedulerrxs(  g & 0 08 DDr1 _TaskResultTceZdZdZdeeddddfdZdeeefdZdd Z dd Z d e d e eee fddfd ZddZddZddZy)CooperativeTaskaU A L{CooperativeTask} is a task object inside a L{Cooperator}, which can be paused, resumed, and stopped. It can also have its completion (or termination) monitored. @see: L{Cooperator.cooperate} @ivar _iterator: the iterator to iterate when this L{CooperativeTask} is asked to do work. @ivar _cooperator: the L{Cooperator} that this L{CooperativeTask} participates in, which is used to re-insert it upon resume. @ivar _deferreds: the list of L{Deferred}s to fire when this task completes, fails, or finishes. @ivar _pauseCount: the number of times that this L{CooperativeTask} has been paused; if 0, it is running. @ivar _completionState: The completion-state of this L{CooperativeTask}. L{None} if the task is not yet completed, an instance of L{TaskStopped} if C{stop} was called to stop this task early, of L{TaskFailed} if the application code in the iterator raised an exception which caused it to terminate, and of L{TaskDone} if it terminated normally via raising C{StopIteration}. iterator cooperator Cooperatorr'Ncz||_||_g|_d|_d|_d|_|j |y)zq A private constructor: to create a new L{CooperativeTask}, see L{Cooperator.cooperate}. rN) _iterator _cooperator _deferreds _pauseCount_completionState_completionResult_addTask)r.rrs r/r0zCooperativeTask.__init__s@"%BD:>SWD!r1ct}|j|jj||S|jJ|j |j|S)a Get a L{Deferred} notification of when this task is complete. @return: a L{Deferred} that fires with the C{iterator} that this L{CooperativeTask} was created with when the iterator has been exhausted (i.e. its C{next} method has raised C{StopIteration}), or fails with the exception raised by C{next} if it raises some other exception. @rtype: L{Deferred} )rrrappendrrYrZs r/whenDonezCooperativeTask.whenDones[/7j  ( OO " "1 %))5 55 JJt-- .r1c|j|xjdz c_|jdk(r|jj|yy)a@ Pause this L{CooperativeTask}. Stop doing work until L{CooperativeTask.resume} is called. If C{pause} is called more than once, C{resume} must be called an equal number of times to resume this task. @raise TaskFinished: if this task has already finished or completed. r@N) _checkFinishrr _removeTaskr^s r/pausezCooperativeTask.pausesH  A   q    ( ( . !r1c|jdk(r t|xjdzc_|jdk(r)|j|jj |yyy)z Resume processing of a paused L{CooperativeTask}. @raise NotPaused: if this L{CooperativeTask} is not paused. rr@N)rrrrrr^s r/resumezCooperativeTask.resumesa   q +  A   q T%:%:%B    % %d +&C r1completionStatedeferredResultc||_||_|js|jj ||j D]}|j |y)a @param completionState: a L{SchedulerError} exception or a subclass thereof, indicating what exception should be raised when subsequent operations are performed. @param deferredResult: the result to fire all the deferreds with. N)rrrrrrrY)r.rrr[s r/ _completeWithzCooperativeTask._completeWithsR!0!/    ( ( . 'A JJ~ & 'r1cz|j|jttty)z Stop further processing of this task. @raise TaskFinished: if this L{CooperativeTask} has previously completed, via C{stop}, completion, or failure. N)rrrrr^s r/r\zCooperativeTask.stops)  ;='+-*@Ar1c4|j |jy)zk If this task has been stopped, raise the appropriate subclass of L{TaskFinished}. N)rr^s r/rzCooperativeTask._checkFinishs!  ,'' ' -r1cx tj}t|tr4j dt ddffd }|j fd|yy#t$r'jtjYyt$r%jtt YywxYw)z Perform one unit of work for this task, retrieving one item from its iterator, stopping if there are no further items in the iterator, and pausing if the result was a L{Deferred}. rer'Nc:jt|yrc)rr)rer.s r/ failLaterz/CooperativeTask._oneWorkUnit..failLaters&&z|W=r1c$jSrc)r)rar.s r/z.CooperativeTask._oneWorkUnit..s 4;;=r1) nextr isinstancerrr addCallbacks StopIterationrr BaseExceptionr)r.rars` r/ _oneWorkUnitzCooperativeTask._oneWorkUnits M$..)F &(+ >w>4>##$@)L ,  ;   xz4>> : 8   z|WY 7 8sA-B9 *B98B9r)rvrrurrrr0rrrrrr rrr\rrrr1r/rrs6 " . "'  '6B(Mr1rc eZdZdZeedfdegegeffdeegdfgefdefdZ dde e d e e e e d e e e fd Zde e d efd Zd ed dfdZd ed dfdZd eefdZddZdZddZddZddZed efdZy)ra Cooperative task scheduler. A cooperative task is an iterator where each iteration represents an atomic unit of work. When the iterator yields, it allows the L{Cooperator} to decide which of its tasks to execute next. If the iterator yields a L{Deferred} then work will pause until the L{Deferred} fires and completes its callback chain. When a L{Cooperator} has more than one task, it distributes work between all tasks. There are two ways to add tasks to a L{Cooperator}, L{cooperate} and L{coiterate}. L{cooperate} is the more useful of the two, as it returns a L{CooperativeTask}, which can be L{paused}, L{resumed} and L{waited on}. L{coiterate} has the same effect, but returns only a L{Deferred} that fires when the task is done. L{Cooperator} can be used for many things, including but not limited to: - running one or more computationally intensive tasks without blocking - limiting parallelism by running a subset of the total tasks simultaneously - doing one thing, waiting for a L{Deferred} to fire, doing the next thing, repeat (i.e. serializing a sequence of asynchronous tasks) Multiple L{Cooperator}s do not cooperate with each other, so for most cases you should use the L{global cooperator}. TterminationPredicateFactory schedulerNstartedcxg|_td|_||_||_d|_d|_||_y)aA Create a scheduler-like object to which iterators may be added. @param terminationPredicateFactory: A no-argument callable which will be invoked at the beginning of each step and should return a no-argument callable which will return True when the step should be terminated. The default factory is time-based and allows iterators to run for 1/100th of a second at a time. @param scheduler: A one-argument callable which takes a no-argument callable and should invoke it at some future point. This will be used to schedule each step of this Cooperator. @param started: A boolean which indicates whether iterators should be stepped as soon as they are added, or if they will be queued up until L{Cooperator.start} is called. rNF)_tasksiter _metarator_terminationPredicateFactory _scheduler _delayedCall_stopped_started)r.rrrs r/r0zCooperator.__init__>s;..0 59"X,G)#48  r1r doneDeferredr'ct| t}t||j}|j||S)a Add an iterator to the list of iterators this L{Cooperator} is currently running. Equivalent to L{cooperate}, but returns a L{Deferred} that will be fired when the task is done. @param doneDeferred: If specified, this will be the Deferred used as the completion deferred. It is suggested that you use the default, which creates a new Deferred for you. @return: a Deferred that will fire when the iterator finishes. )rrr chainDeferred)r.rrrs r/ coiteratezCooperator.coiterate]s@$  #:L5D d6 (*  |,r1ct||S)a  Start running the given iterator as a long-running cooperative task, by calling next() on it as a periodic timed event. @param iterator: the iterator to invoke. @return: a L{CooperativeTask} object representing this task. )r)r.rs r/ cooperatezCooperator.cooperatewsx..r1taskc|jrG|jj||jt t t y|jj||j y)zH Add a L{CooperativeTask} object to this L{Cooperator}. N)rrrrrr _rescheduler.rs r/rzCooperator._addTasksX == KK  t $   /17;K;M3N O KK  t $    r1c|jj||js/|jr"|jjd|_yyy)zF Remove a L{CooperativeTask} from this L{Cooperator}. N)rremoverrXrs r/rzCooperator._removeTasksF 4 {{t00    $ $ & $D  1{r1c#K|j}|jrF|jD]}||syt|j|_|jrEyyw)z Yield all L{CooperativeTask} objects in a loop as long as this L{Cooperator}'s termination condition has not been met. N)rrrr)r. terminatorrMs r/_tasksWhileNotStoppedz Cooperator._tasksWhileNotStoppedsX 668 kk__ < #4;;/DO kks7A&)A&$A&c|d|_|jD]}|j|jy)z) Run one scheduler tick. N)rrrrr.taskObjs r/_tickzCooperator._ticks<!113 #G  " # r1Fc|jsd|_y|j.|jr!|j |j |_yyyNT)r_mustScheduleOnStartrrrrr^s r/rzCooperator._reschedulesC}}(,D %     $ $ ;D *5 $r1c^d|_d|_|jr|`|jyy)z) Begin scheduling steps. FTN)rrrrr^s r/rVzCooperator.starts2   $ $)     %r1cd|_|jD]-}|jtt t/g|_|j "|j j d|_yy)z Stop scheduling steps. Errback the completion Deferreds of all iterators which have been added and forget about them. TN)rrrrrrrXrs r/r\zCooperator.stopso  {{ SG  ! !"2"4g>N>P6Q R S    (    $ $ & $D  )r1c8|jxr |j S)z Is this L{Cooperator} is currently running? @return: C{True} if the L{Cooperator} is running, C{False} otherwise. @rtype: C{bool} )rrr^s r/rSzCooperator.runnings}}2T]]!22r1rcr)rvrrurrrrrrr0rrr rrrrrrrrrrrrVr\rrSrr1r/rrs,DIOBS  %-b(2t82D.D%E Xb$h/0,>?   DDH<(x(>?@ (<( ) 4 /(<"8 /_ / _  %%D% 0x'@ 0!< %333r1rrc,tj|S)a% Cooperatively iterate over the given iterator, dividing runtime between it and all other iterators which have been passed to this function and not yet exhausted. @param iterator: the iterator to invoke. @return: a Deferred that will fire when the iterator finishes. )_theCooperatorrrs r/rrs  # #H --r1c,tj|S)aS Start running the given iterator as a long-running cooperative task, by calling next() on it as a periodic timed event. This is very useful if you have computationally expensive tasks that you want to run without blocking the reactor. Just break each task up so that it yields frequently, pass it in here and the global L{Cooperator} will make sure work is distributed between them without blocking longer than a single iteration of a single task. @param iterator: the iterator to invoke. @return: a L{CooperativeTask} object representing this task. )rrrs r/rrs  # #H --r1c eZdZdZdZddZdefdZddZded e d e fd e d e de f d Z de e fdZdeddfdZdeeddfdZy)Clockz Provide a deterministic, easily-controlled implementation of L{IReactorTime.callLater}. This is commonly useful for writing deterministic unit tests for code which schedules events using this API. gr'Ncg|_yrccallsr^s r/r0zClock.__init__s (* r1c|jS)a  Pretend to be time.time(). This is used internally when an operation such as L{IDelayedCall.reset} needs to determine a time value relative to the current time. @return: The time which should be considered the current time. )rightNowr^s r/rAz Clock.secondss}}r1c>|jjdy)zR Sort the pending calls according to the time they are scheduled. c"|jSrc)getTime)r%s r/rz"Clock._sortCalls..s aiikr1)keyN)rsortr^s r/ _sortCallszClock._sortCallss 12r1delayr.argsr&c t|j|z||||jjd|j}|jj ||j |S)zL See L{twisted.internet.interfaces.IReactorTime.callLater}. cyrcr)cs r/rz!Clock.callLater..$sr1)rrArrrr)r.rrrr&dcs r/rszClock.callLaters_  LLNU "    JJ    LL  "  r1c|jS)zQ See L{twisted.internet.interfaces.IReactorTime.getDelayedCalls} rr^s r/getDelayedCallszClock.getDelayedCalls+szzr1amountc|xj|z c_|j|jr|jdj|j kr|jj d}d|_|j|ji|j|j|jr0|jdj|j kryyyy)z Move time on this clock forward by the given amount and run whatever pending calls should be run. @param amount: The number of seconds which to advance this clock's time. rr@N) rrrrrApopcalledr|rr&)r.r rs r/advancez Clock.advance1s   jjTZZ]224 F::>>!$DDK DIItyy ,DGG , OO  jjTZZ]224 FjFjr1timingsc4|D]}|j|y)zB Advance incrementally by the given set of times. N)r)r.rr s r/pumpz Clock.pumpAs !F LL  !r1r)rvrrurrr0rrrArrrJrrsr r rrrrr1r/rrs H+3 &.sF{&;DJRX &,!7 e !HUO!!r1rr-r.rr&cdttddffd }dtdtffd }t|}|j||j ||j d|S)a+ Call the given function after a certain period of time has passed. @param clock: The object which will be used to schedule the delayed call. @param delay: The number of seconds to wait before calling the function. @param callable: The callable to call after the delay, or C{None}. @param args: The positional arguments to pass to C{callable}. @param kw: The keyword arguments to pass to C{callable}. @return: A deferred that fires with the result of the callable when the specified time has elapsed. r<r'Nc&jyrc)rX)r< delayedCalls r/deferLaterCancelz$deferLater..deferLaterCancelbsr1racyiSrcr)rarrr&s r/rdzdeferLater..cbes  $$$r1)rrJrrirsrY) r-rrrr&rrdr[rs ``` @r/ deferLaterrIsb28F#3%6%b% /0AMM"//%T:K Hr1mainz Deferred[_T]argv_reactorcP ddlm}tt|t |g|}dd d fd }j dd|d t d tddffd d t ddf fd }|j|jtjy)au Call C{main} and run the reactor until the L{Deferred} it returns fires or the coroutine it returns completes. This is intended as the way to start up an application with a well-defined completion condition. Use it to write clients or one-off asynchronous operations. Prefer this to calling C{reactor.run} directly, as this function will also: - Take care to call C{reactor.stop} once and only once, and at the right time. - Log any failures from the C{Deferred} returned by C{main}. - Exit the application when done, with exit code 0 in case of success and 1 in case of failure. If C{main} fails with a C{SystemExit} error, the code returned is used. The following demonstrates the signature of a C{main} function which can be used with L{react}:: async def main(reactor, username, password): return "ok" task.react(main, ("alice", "secret")) @param main: A callable which returns a L{Deferred} or coroutine. It should take the reactor as its first parameter, followed by the elements of C{argv}. @param argv: A list of arguments to pass to C{main}. If omitted the callable will be invoked with no additional arguments. @param _reactor: An implementation detail to allow easier unit testing. Do not supply this parameter. @since: 12.3 Nrr*Fr'c dyrr)stoppingsr/ onShutdownzreact..onShutdownsr1beforeshutdownra stopReactorc|rJ jt|trE|j t |j jytj|ddyy#t$rYawxYw)Nzmain function encountered errorr@) r\rrrcheck SystemExitvaluecodererr)rar"rr's r/r\zreact..stopsw ' ''   fg &||J'3||(( AB '%  sA// A;:A;cLr |dyJj|dy)NFT)callWhenRunning)rarr\rs r/cbFinishzreact..cbFinishs.   ' ''  $ $T64 8r1r) r,r+r rraddSystemEventTriggerrJraddBothrunsysexit) rrrr+finishedrr+r'r\rs ` @@@r/reactr2psX, g.d83d34H DH ""8ZDV$4 99D9 X LLNHHTNr1)rrrrrrr2rc)rN)@rr/rr8typingrrrrrrr r r r r zope.interfacer incrementalrtwisted.internet.basertwisted.internet.deferrrrtwisted.internet.errorrtwisted.internet.interfacesrrrtwisted.pythonrrtwisted.python.deprecatertwisted.python.failurerrr ExceptionrrrrrrrrrrrrrrrrrrrrJrr2__all__rr1r/r?s:       '-JJ4PP'A* T]M M `Y~>| , '' ET 2E|E ~& YMYMxy3y3x . .(2,)? .. ..$ \J!J!J!`-1$ $ $ xR()$   $  $ b\ $ X '+ U   hrlInfb&@AAB D U 6  U |$ UUp r1