source: vendor/python/2.5/Doc/lib/libsched.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.4 KB
Line 
1\section{\module{sched} ---
2 Event scheduler}
3
4% LaTeXed and enhanced from comments in file
5
6\declaremodule{standard}{sched}
7\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
8\modulesynopsis{General purpose event scheduler.}
9
10The \module{sched} module defines a class which implements a general
11purpose event scheduler:\index{event scheduling}
12
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
17a number (the ``time'', in any units whatsoever). The \var{delayfunc}
18function 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
47\subsection{Scheduler Objects \label{scheduler-objects}}
48
49\class{scheduler} instances have the following methods:
50
51\begin{methoddesc}{enterabs}{time, priority, action, argument}
52Schedule a new event. The \var{time} argument should be a numeric type
53compatible with the return value of the \var{timefunc} function passed
54to the constructor. Events scheduled for
55the same \var{time} will be executed in the order of their
56\var{priority}.
57
58Executing the event means executing
59\code{\var{action}(*\var{argument})}. \var{argument} must be a
60sequence holding the parameters for \var{action}.
61
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}{}
79Return true if the event queue is empty.
80\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
95events will be dropped; the calling code is responsible for canceling
96events which are no longer pertinent.
97\end{methoddesc}
Note: See TracBrowser for help on using the repository browser.