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