U vi@sddlZddlZddlmZddlmZmZmZddlm Z mZddl m Z ddl m Z ddlmZddlmZdd lmZd d lmZd d lmZed ZedZGdddee jZdS)N)deque)DequeOptionalTypeVar)abctyping) Disposable)default_thread_factory) DELTA_ZERO)DisposedException) PriorityQueue)PeriodicScheduler) ScheduledItemZRx_TStatecseZdZdZdeejeddfdd Zdej e ee e j ddd Z dejej e ee e j d d d Zdejej e ee e j d d dZdejeje ee e j dfdd ZedddZddddZddddZddddZZS) EventLoopSchedulerzFCreates an object that schedules units of work on a designated thread.NF)thread_factory exit_if_emptyreturncsJtd|_|pt|_d|_tt|_ t |_ t |_ ||_dS)NF)super__init__ _is_disposedr _thread_factory_thread threading ConditionLock _conditionr _queuer _ready_list_exit_if_empty)selfrr __class__W/opt/alt/python38/lib/python3.8/site-packages/reactivex/scheduler/eventloopscheduler.pyrs zEventLoopScheduler.__init__)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_absolutenow)r!r&r'r$r$r%schedule*szEventLoopScheduler.schedule)duetimer&r'rcCs$tt||}||j|||S)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). )maxr Z to_timedeltar)r*)r!r,r&r'r$r$r%schedule_relative:sz$EventLoopScheduler.schedule_relativec Csv|jr t||}t||||}|j<||jkrD|j|n |j ||j | W5QRXt |j S)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). )rr to_datetimerrr*rappendrenqueuenotify_ensure_threadrcancel)r!r,r&r'dtsir$r$r%r)Os    z$EventLoopScheduler.schedule_absolute)periodr&r'rcs|jr ttj|||dS)aSchedules a periodic piece of work. Args: period: Period in seconds or timedelta for running the work periodically. action: Action to be executed. state: [Optional] Initial state passed to the action upon the first iteration. Returns: The disposable object used to cancel the scheduled recurring action (best effort). r()rr rschedule_periodic)r!r7r&r'r"r$r%r8qsz$EventLoopScheduler.schedule_periodic)rc Cs0|j |j o|jdk W5QRSQRXdS)z0Checks if there is an event loop thread running.N)rrrr!r$r$r% _has_threadszEventLoopScheduler._has_threadcCs$|js ||j}||_|dS)zWEnsures there is an event loop thread running. Should be called under the gate.N)rrrunstart)r!threadr$r$r%r3s z!EventLoopScheduler._ensure_threadc CsZt}|j|jr"W5QRdS|j}|jr~|jj}|jrb||jdjkrb||j q:||krlq~||j q(|jr||j q~W5QRX|r| }| s| q|j|jrW5QRqnp|jr$|j}|j}|j| }|dkrJtd||j|n&|jr@d|_W5QRdS|jW5QRXqdS)zEvent loop scheduled on the designated event loop thread. The loop is suspended/resumed using the condition which gets notified by calls to Schedule or calls to dispose.Nrz timeout: %s)rrrr*rpeekr,rr0popleftdequeueZ is_cancelledZinvoke total_secondslogdebugwaitr r)r!readytimeZdueitemsecondsr$r$r%r;s@     zEventLoopScheduler.runc Cs,|j|jsd|_|jW5QRXdS)zxEnds the thread associated with this scheduler. All remaining work in the scheduler queue is abandoned. TN)rrr2r9r$r$r%disposeszEventLoopScheduler.dispose)NF)N)N)N)N)__name__ __module__ __qualname____doc__rrZStartableFactoryboolrZScheduledActionrrDisposableBaser+Z RelativeTimer.Z AbsoluteTimer)ZScheduledPeriodicActionr8r:r3r;rI __classcell__r$r$r"r%rsN   & :r)loggingr collectionsrrrrrZ reactivexrZreactivex.disposablerZreactivex.internal.concurrencyr Zreactivex.internal.constantsr Zreactivex.internal.exceptionsr Z reactivex.internal.priorityqueuer ZperiodicschedulerrZ scheduleditemr getLoggerrBrrOrr$r$r$r%s