blob: baa2ffe730794f0059213d58a06018c611609bf2 [file] [log] [blame]
Fred Drake7e990321999-06-25 18:52:44 +00001% LaTeXed and enhanced from comments in file
2\section{\module{sched} ---
3 Event scheduler}
4
5\declaremodule{standard}{sched}
6\sectionauthor{Moshe Zadka}{mzadka@geocities.com}
7\modulesynopsis{General purpose event scheduler.}
8
9The \module{sched} module defines a class which implements a general
Fred Drake14ae4b41999-06-25 19:13:36 +000010purpose event scheduler:\index{event scheduling}
Fred Drake7e990321999-06-25 18:52:44 +000011
12\begin{classdesc}{scheduler}{timefunc, delayfunc}
13The \class{scheduler} class defines a generic interface to scheduling
14events. It needs two functions to actually deal with the ``outside world''
15--- \var{timefunc} should be callable without arguments, and return
Fred Drake14ae4b41999-06-25 19:13:36 +000016a number (the ``time'', in any units whatsoever). The \var{delayfunc}
Fred Drake7e990321999-06-25 18:52:44 +000017function should be callable with one argument, compatible with the output
18of \var{timefunc}, and should delay that many time units.
19\var{delayfunc} will also be called with the argument \code{0} after
20each event is run to allow other threads an opportunity to run in
21multi-threaded applications.
22\end{classdesc}
23
24Example:
25
26\begin{verbatim}
27>>> import sched, time
28>>> s=sched.scheduler(time.time, time.sleep)
29>>> def print_time(): print "From print_time", time.time()
30...
31>>> def print_some_times():
32... print time.time()
33... s.enter(5, 1, print_time, ())
34... s.enter(10, 1, print_time, ())
35... s.run()
36... print time.time()
37...
38>>> print_some_times()
39930343690.257
40From print_time 930343695.274
41From print_time 930343700.273
42930343700.276
43\end{verbatim}
44
45
Fred Drake14ae4b41999-06-25 19:13:36 +000046\subsection{Scheduler Objects \label{scheduler-objects}}
Fred Drake7e990321999-06-25 18:52:44 +000047
Fred Drake14ae4b41999-06-25 19:13:36 +000048\class{scheduler} instances have the following methods:
Fred Drake7e990321999-06-25 18:52:44 +000049
50\begin{methoddesc}{enterabs}{time, priority, action, argument}
51Schedule a new event. The \var{time} argument should be a numeric type
52compatible to the return value of \var{timefunc}. Events scheduled for
53the same \var{time} will be executed in the order of their
54\var{priority}.
55
56Executing the event means executing \code{apply(\var{action},
57\var{argument})}. \var{argument} must be a tuple holding the
58parameters for \var{action}.
59
60Return value is an event which may be used for later cancellation of
61the event (see \method{cancel()}).
62\end{methoddesc}
63
64\begin{methoddesc}{enter}{delay, priority, action, argument}
65Schedule an event for \var{delay} more time units. Other then the
66relative time, the other arguments, the effect and the return value
67are the same as those for \method{enterabs()}.
68\end{methoddesc}
69
70\begin{methoddesc}{cancel}{event}
71Remove the event from the queue. If \var{event} is not an event
72currently in the queue, this method will raise a
73\exception{RuntimeError}.
74\end{methoddesc}
75
76\begin{methoddesc}{empty}{}
Fred Drake14ae4b41999-06-25 19:13:36 +000077Return true if the event queue is empty.
Fred Drake7e990321999-06-25 18:52:44 +000078\end{methoddesc}
79
80\begin{methoddesc}{run}{}
81Run all scheduled events. This function will wait
82(using the \function{delayfunc} function passed to the constructor)
83for the next event, then execute it and so on until there are no more
84scheduled events.
85
86Either \var{action} or \var{delayfunc} can raise an exception. In
87either case, the scheduler will maintain a consistent state and
88propagate the exception. If an exception is raised by \var{action},
89the event will not be attempted in future calls to \method{run()}.
90
91If a sequence of events takes longer to run than the time available
92before the next event, the scheduler will simply fall behind. No
93events will be dropped; the calling code is responsible for cancelling
94events which are no longer pertinent.
95\end{methoddesc}