A scheduler that can run a run_sync: flushed once, firing no timer,
with nothing waiting on a loop, as MixedScheduler("sync") in
runSync, whose timers do fire but too late for the run. Each Effect
fiber has its own dispatcher and runSync flushes its root's alone, so
while one runs, what another fiber schedules waits for the next time the
scheduler is driven: a child that yields is asynchronous.
Attributes#
| Attribute | Type | Description |
|---|---|---|
root | object = None |
Functions#
__init__#
__init__() -> Nonedef __init__(self) -> None:
super().__init__()
self.root: object = None
self._held: list[tuple[Task, int]] = []schedule#
schedule(task, priority=0) -> None| Parameter | Type | Description |
|---|---|---|
task | Task | |
priority | int = 0 |
def schedule(self, task: Task, priority: int = 0) -> None:
if self.root is not None:
from pyfect._fiber import current_fiber, current_run_scheduler
if current_run_scheduler() is self and current_fiber() is not self.root:
self._held.append((task, priority))
return
super().schedule(task, priority)schedule_as_root#
schedule_as_root(task) -> NoneA task on the root's dispatcher, whichever fiber is running.
| Parameter | Type | Description |
|---|---|---|
task | Task |
def schedule_as_root(self, task: Task) -> None:
"""A task on the root's dispatcher, whichever fiber is running."""
super().schedule(task)flush_as#
flush_as(root) -> Noneflush, with root as the run's root fiber.
| Parameter | Type | Description |
|---|---|---|
root | object |
def flush_as(self, root: object) -> None:
"""``flush``, with ``root`` as the run's root fiber."""
self.root = root
try:
self.flush()
finally:
self.root = None
held, self._held = self._held, []
for task, priority in held:
super().schedule(task, priority)runs_here#
runs_here() -> boolWhether this thread is the one running the scheduler's tasks.
Returns bool
def runs_here(self) -> bool:
"""Whether this thread is the one running the scheduler's tasks."""
adopter = self._adopter
if adopter is not None:
return adopter.runs_here()
return self._driver == threading.get_ident()driver#
driver() -> int | NoneThe thread running this scheduler's tasks now. Once adopted, the adopter's thread, while a task of this scheduler is queued or running there: idle, it races with nobody.
Returns int | None
def driver(self) -> int | None:
"""The thread running this scheduler's tasks now. Once adopted, the
adopter's thread, while a task of this scheduler is queued or running
there: idle, it races with nobody."""
adopter = self._adopter
if adopter is None:
return self._driver
return adopter.driver() if self._forwarded else Noneoutside_work#
outside_work() -> WakeTrackerCounts fibers waiting on work handed to another thread, and the tasks their resumes schedule.
Returns WakeTracker
def outside_work(self) -> WakeTracker:
"""Counts fibers waiting on work handed to another thread, and the
tasks their resumes schedule."""
return self._outsidetrack#
track(tracker, f) -> NoneRun f, counting what it schedules against tracker.
| Parameter | Type | Description |
|---|---|---|
tracker | WakeTracker | |
f | Callable[[], None] |
def track(self, tracker: WakeTracker, f: Callable[[], None]) -> None:
"""Run ``f``, counting what it schedules against ``tracker``."""
previous = self._tracking.tracker
self._tracking.tracker = tracker
try:
f()
finally:
self._tracking.tracker = previousadopt#
adopt(into) -> NoneHand what is left to into for good: the tasks ready now, the timers
pending, and every task or timer scheduled from now on. The run whose caller
drove this scheduler has returned, and its fibers go on (Effect's scheduler is
always alive).
| Parameter | Type | Description |
|---|---|---|
into | SyncScheduler |
def adopt(self, into: SyncScheduler) -> None:
"""Hand what is left to ``into`` for good: the tasks ready now, the timers
pending, and every task or timer scheduled from now on. The run whose caller
drove this scheduler has returned, and its fibers go on (Effect's scheduler is
always alive)."""
with self._cond:
if self._adopter is not None:
return
self._adopter = into
batch = self._take_batch() if self._ready or self._prioritized else deque[Task]()
timers = [entry for _, _, entry in self._timers.entries if entry.owner is not None]
for task in batch:
into.schedule(self._forward(task))
for entry in timers:
timer = entry.value
if timer is None:
continue
moved = into.call_at(entry.when, self._counted(self._fire(entry)), daemon=timer.daemon)
with self._cond:
cancelled = entry.owner is None
if not cancelled:
self._moved[entry] = moved
if cancelled: # while it moved
moved()call_later#
call_later(delay, task, *, daemon=False) -> Cancel| Parameter | Type | Description |
|---|---|---|
delay | Duration | |
task | Task | |
daemon | bool = False |
Returns Cancel
def call_later(self, delay: Duration, task: Task, *, daemon: bool = False) -> Cancel:
return self.call_at(time.monotonic() + max(delay.to_seconds(), 0.0), task, daemon=daemon)call_at#
call_at(when, task, *, daemon=False) -> Cancelcall_later at when on the time.monotonic clock.
| Parameter | Type | Description |
|---|---|---|
when | float | |
task | Task | |
daemon | bool = False |
Returns Cancel
def call_at(self, when: float, task: Task, *, daemon: bool = False) -> Cancel:
"""``call_later`` at ``when`` on the ``time.monotonic`` clock."""
with self._cond:
adopter = self._adopter
if adopter is None:
entry = self._timers.push(when, _Timer(task, daemon=daemon))
self._cond.notify()
else:
entry = None
if entry is None:
assert adopter is not None
return adopter.call_at(when, self._counted(task), daemon=daemon)
def cancel() -> None:
with self._cond:
entry.cancel()
moved = self._moved.pop(entry, None)
if moved is not None: # ``adopt`` moved it: the adopter lets go of it too
moved()
return cancelrun_until#
run_until(done, *, stall_timeout=None) -> NoneRun tasks and timers until done() is true.
Blocks while nothing is ready. With stall_timeout, raises
StalledError once nothing has been ready, no timer but daemon
timers has been pending, and no other thread has scheduled anything
for that long.
A KeyboardInterrupt raised while blocked propagates; the caller
decides how to unwind.
| Parameter | Type | Description |
|---|---|---|
done | Callable[[], bool] | |
stall_timeout | float | None = None |
def run_until(self, done: Callable[[], bool], *, stall_timeout: float | None = None) -> None:
"""Run tasks and timers until ``done()`` is true.
Blocks while nothing is ready. With ``stall_timeout``, raises
``StalledError`` once nothing has been ready, no timer but daemon
timers has been pending, and no other thread has scheduled anything
for that long.
A ``KeyboardInterrupt`` raised while blocked propagates; the caller
decides how to unwind.
"""
if self._adopter is not None:
raise RuntimeError("run_until on an adopted scheduler: its tasks run elsewhere")
self._last_activity = time.monotonic()
driver, self._driver = self._driver, threading.get_ident()
try:
self._run_until(done, stall_timeout)
finally:
self._driver = driverflush#
flush() -> NoneRun what is ready, and what that schedules, without waiting or
firing a timer: run_blocking's drain (Effect's dispatcher.flush).
def flush(self) -> None:
"""Run what is ready, and what that schedules, without waiting or
firing a timer: ``run_blocking``'s drain (Effect's ``dispatcher.flush``)."""
driver, self._driver = self._driver, threading.get_ident()
try:
while self._ready or self._prioritized:
with self._cond:
batch = self._take_batch()
self._run_tasks(batch)
finally:
self._driver = driver