PyDocs

Nav

Concurrent Execution

scheduler

sched

class (source)
  • scheduler(timefunc=time.monotonic, delayfunc=time.sleep)
The scheduler class defines a generic interface to scheduling events. It needs two functions to actually deal with the “outside world” — timefunc should be callable without arguments, and return a number (the “time”, in any units whatsoever). The delayfunc function should be callable with one argument, compatible with the output of timefunc, and should delay that many time units. delayfunc will also be called with the argument 0 after each event is run to allow other threads an opportunity to run in multi-threaded applications.
Changed in 3.3
  • 3.3: timefunc and delayfunc parameters are optional.
  • 3.3: scheduler class can be safely used in multi-threaded environments.
Members of scheduler

c

cancel
Remove an event from the queue.

e

empty
Check whether the queue is empty.
enter
A variant that specifies the time as a relative time.
enterabs
Enter a new event in the queue at an absolute time.

q

queue
An ordered list of upcoming events.

r

run
Execute events until the queue is empty.
1 Example
>>> import sched, time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time(a='default'):
...     print("From print_time", time.time(), a)
...
>>> def print_some_times():
...     print(time.time())
...     s.enter(10, 1, print_time)
...     s.enter(5, 2, print_time, argument=('positional',))
...     # despite having higher priority, 'keyword' runs after 'positional' as enter() is relative
...     s.enter(5, 1, print_time, kwargs={'a': 'keyword'})
...     s.enterabs(1_650_000_000, 10, print_time, argument=("first enterabs",))
...     s.enterabs(1_650_000_000, 5, print_time, argument=("second enterabs",))
...     s.run()
...     print(time.time())
...
>>> print_some_times()
1652342830.3640375
From print_time 1652342830.3642538 second enterabs
From print_time 1652342830.3643398 first enterabs
From print_time 1652342835.3694863 positional
From print_time 1652342835.3696074 keyword
From print_time 1652342840.369612 default
1652342840.3697174
from the Python docs
See Also
No see-alsos for sched/scheduler
0 Notes
No notes for scheduler