U vi @sddlZddlZddlmZmZddlmZmZmZddlm Z mZddl m Z ddl m Z mZddlmZdd lmZed Zd Zed ZGd ddeZdS)N)datetime timedelta)AnyOptionalTypeVar)abctyping) AbsoluteTime)ArgumentOutOfRangeException PriorityQueue)PeriodicScheduler) ScheduledItemZRxd_TStatecs,eZdZdZd#eddfdd Zejddd Zeed Z ee dd d Z d$ej e ee ejd ddZd%ejej e ee ejdddZd&ejej e ee ejdddZedddZddddZejddddZejddddZejddddZeejejejd d!d"ZZS)'VirtualTimeSchedulerzcVirtual Scheduler. This scheduler should work with either datetime/timespan or ticks as int/intrN) initial_clockreturncs,t||_d|_t|_t|_dS)zCreates a new virtual time scheduler with the specified initial clock value. Args: initial_clock: Initial value for the clock. FN) super__init___clock _is_enabled threadingLock_lockr _queue)selfr __class__Y/opt/alt/python38/lib/python3.8/site-packages/reactivex/scheduler/virtualtimescheduler.pyrs   zVirtualTimeScheduler.__init__)rc Cs$|j|jW5QRSQRXdS)N)rrrrrr _get_clock&szVirtualTimeScheduler._get_clock)fgetcCs ||jS)zRepresents a notion of time for this scheduler. Tasks being scheduled on a scheduler will adhere to the time denoted by this property. Returns: The scheduler's current time, as a datetime instance. ) to_datetimerr!rrr now,s zVirtualTimeScheduler.now)actionstatercCs|j|j||dS)a!Schedules an action to be executed. Args: action: Action to be executed. state: [Optional] state to be given to the action function. Returns: The disposable object used to cancel the scheduled action (best effort). r')schedule_absoluter)rr&r'rrr schedule8szVirtualTimeScheduler.schedule)duetimer&r'rcCs||j|}|j|||dS)auSchedules an action to be executed after duetime. Args: duetime: Relative time after which to execute the action. action: Action to be executed. state: [Optional] state to be given to the action function. Returns: The disposable object used to cancel the scheduled action (best effort). r()addrr))rr+r&r'timerrr schedule_relativeHsz&VirtualTimeScheduler.schedule_relativec Cs<||}t||||}|j|j|W5QRX|jS)aoSchedules an action to be executed at duetime. Args: duetime: Absolute time at which to execute the action. action: Action to be executed. state: [Optional] state to be given to the action function. Returns: The disposable object used to cancel the scheduled action (best effort). )r$rrrenqueueZ disposable)rr+r&r'dtsirrr r)]s  z&VirtualTimeScheduler.schedule_absolutec Cs|j |jrW5QRdSd|_W5QRXd}|j|jrD|jsPW5QRq|j}|j|jkrt|jtr||j|_n| |j|_d}n<|t krt|jtr|j t dd7_ n|jd7_d}W5QRX| s||d7}q0|dS)z"Starts the virtual time scheduler.NTri) microsecondsg?r )rrrdequeuer+r% isinstancerr to_seconds MAX_SPINNINGclockr is_cancelledinvokestop)rZspinningitemrrr startus.        zVirtualTimeScheduler.startc Cs|j d|_W5QRXdS)z!Stops the virtual time scheduler.FN)rrr!rrr r:szVirtualTimeScheduler.stop)r-rc Cs$||}|j:|j|kr"t|j|ks2|jr@W5QRdSd|_W5QRX|jx|jrd|jspW5QRq|j}|j|krW5QRq|j|jkrt|j t r|j|_ n| |j|_ |j W5QRX| sP|qP|j.d|_t|j t r ||_ n | ||_ W5QRXdS)zAdvances the schedulers clock to the specified absolute time, running all work til that point. Args: time: Absolute time to advance the schedulers clock to. NTF)r$rr%r rrpeekr+r4rrr5r3r8r9)rr-r0r;rrr advance_tos2           zVirtualTimeScheduler.advance_tocCs*td||||j||dS)zAdvances the schedulers clock by the specified relative time, running all work scheduled for that timespan. Args: time: Relative time to advance the schedulers clock by. z(VirtualTimeScheduler.advance_by(time=%s)N)logdebugr>r,r% to_timedelta)rr-rrr advance_bys zVirtualTimeScheduler.advance_byc Csd||j||}||}|j|kr.t|j&t|jtrJ||_n | ||_W5QRXdS)zAdvances the schedulers clock by the specified relative time. Args: time: Relative time to advance the schedulers clock by. N) r,r%rAr$r rr4rrr5)rr-absoluter0rrr sleeps   zVirtualTimeScheduler.sleep)rCrelativercCs||||S)aAdds a relative time value to an absolute time value. Args: absolute: Absolute virtual time value. relative: Relative virtual time value to add. Returns: The resulting absolute virtual time sum value. )r$rA)clsrCrErrr r,szVirtualTimeScheduler.add)r)N)N)N)__name__ __module__ __qualname____doc__r rrr"propertyr7rr%ZScheduledActionrrrZDisposableBaser*Z RelativeTimer.r)rr<r:r>rBrD classmethodr, __classcell__rrrr rsH     %. r)loggingrrrrrrrZ reactivexrZreactivex.abc.schedulerr Zreactivex.internalr r Zperiodicschedulerr Z scheduleditemr getLoggerr?r6rrrrrr s