~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.2-docs-html/library/sched.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>17.6. sched — Event scheduler &mdash; Python 3.5.2 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '3.5.2',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 3.5.2 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
 
33
    <link rel="up" title="17. Concurrent Execution" href="concurrency.html" />
 
34
    <link rel="next" title="17.7. queue — A synchronized queue class" href="queue.html" />
 
35
    <link rel="prev" title="17.5. subprocess — Subprocess management" href="subprocess.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    
 
38
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
39
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
40
    
 
41
    
 
42
 
 
43
 
 
44
  </head>
 
45
  <body role="document">  
 
46
    <div class="related" role="navigation" aria-label="related navigation">
 
47
      <h3>Navigation</h3>
 
48
      <ul>
 
49
        <li class="right" style="margin-right: 10px">
 
50
          <a href="../genindex.html" title="General Index"
 
51
             accesskey="I">index</a></li>
 
52
        <li class="right" >
 
53
          <a href="../py-modindex.html" title="Python Module Index"
 
54
             >modules</a> |</li>
 
55
        <li class="right" >
 
56
          <a href="queue.html" title="17.7. queue — A synchronized queue class"
 
57
             accesskey="N">next</a> |</li>
 
58
        <li class="right" >
 
59
          <a href="subprocess.html" title="17.5. subprocess — Subprocess management"
 
60
             accesskey="P">previous</a> |</li>
 
61
        <li><img src="../_static/py.png" alt=""
 
62
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
63
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
64
        <li>
 
65
          <span class="version_switcher_placeholder">3.5.2</span>
 
66
          <a href="../index.html">Documentation </a> &raquo;
 
67
        </li>
 
68
 
 
69
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
70
          <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">17. Concurrent Execution</a> &raquo;</li>
 
71
    <li class="right">
 
72
        
 
73
 
 
74
    <div class="inline-search" style="display: none" role="search">
 
75
        <form class="inline-search" action="../search.html" method="get">
 
76
          <input placeholder="Quick search" type="text" name="q" />
 
77
          <input type="submit" value="Go" />
 
78
          <input type="hidden" name="check_keywords" value="yes" />
 
79
          <input type="hidden" name="area" value="default" />
 
80
        </form>
 
81
    </div>
 
82
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
83
         |
 
84
    </li>
 
85
 
 
86
      </ul>
 
87
    </div>    
 
88
 
 
89
    <div class="document">
 
90
      <div class="documentwrapper">
 
91
        <div class="bodywrapper">
 
92
          <div class="body" role="main">
 
93
            
 
94
  <div class="section" id="module-sched">
 
95
<span id="sched-event-scheduler"></span><h1>17.6. <a class="reference internal" href="#module-sched" title="sched: General purpose event scheduler."><code class="xref py py-mod docutils literal"><span class="pre">sched</span></code></a> &#8212; Event scheduler<a class="headerlink" href="#module-sched" title="Permalink to this headline">¶</a></h1>
 
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/sched.py">Lib/sched.py</a></p>
 
97
<hr class="docutils" id="index-0" />
 
98
<p>The <a class="reference internal" href="#module-sched" title="sched: General purpose event scheduler."><code class="xref py py-mod docutils literal"><span class="pre">sched</span></code></a> module defines a class which implements a general purpose event
 
99
scheduler:</p>
 
100
<dl class="class">
 
101
<dt id="sched.scheduler">
 
102
<em class="property">class </em><code class="descclassname">sched.</code><code class="descname">scheduler</code><span class="sig-paren">(</span><em>timefunc=time.monotonic</em>, <em>delayfunc=time.sleep</em><span class="sig-paren">)</span><a class="headerlink" href="#sched.scheduler" title="Permalink to this definition">¶</a></dt>
 
103
<dd><p>The <a class="reference internal" href="#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal"><span class="pre">scheduler</span></code></a> class defines a generic interface to scheduling events.
 
104
It needs two functions to actually deal with the &#8220;outside world&#8221; &#8212; <em>timefunc</em>
 
105
should be callable without arguments, and return  a number (the &#8220;time&#8221;, in any
 
106
units whatsoever). If time.monotonic is not available, the <em>timefunc</em> default
 
107
is time.time instead. The <em>delayfunc</em> function should be callable with one
 
108
argument, compatible with the output of <em>timefunc</em>, and should delay that many
 
109
time units. <em>delayfunc</em> will also be called with the argument <code class="docutils literal"><span class="pre">0</span></code> after each
 
110
event is run to allow other threads an opportunity to run in multi-threaded
 
111
applications.</p>
 
112
<div class="versionchanged">
 
113
<p><span class="versionmodified">Changed in version 3.3: </span><em>timefunc</em> and <em>delayfunc</em> parameters are optional.</p>
 
114
</div>
 
115
<div class="versionchanged">
 
116
<p><span class="versionmodified">Changed in version 3.3: </span><a class="reference internal" href="#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal"><span class="pre">scheduler</span></code></a> class can be safely used in multi-threaded
 
117
environments.</p>
 
118
</div>
 
119
</dd></dl>
 
120
 
 
121
<p>Example:</p>
 
122
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sched</span><span class="o">,</span> <span class="nn">time</span>
 
123
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">sched</span><span class="o">.</span><span class="n">scheduler</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">)</span>
 
124
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">print_time</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="s1">&#39;default&#39;</span><span class="p">):</span>
 
125
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;From print_time&quot;</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">(),</span> <span class="n">a</span><span class="p">)</span>
 
126
<span class="gp">...</span>
 
127
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">print_some_times</span><span class="p">():</span>
 
128
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
 
129
<span class="gp">... </span>    <span class="n">s</span><span class="o">.</span><span class="n">enter</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">print_time</span><span class="p">)</span>
 
130
<span class="gp">... </span>    <span class="n">s</span><span class="o">.</span><span class="n">enter</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">print_time</span><span class="p">,</span> <span class="n">argument</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;positional&#39;</span><span class="p">,))</span>
 
131
<span class="gp">... </span>    <span class="n">s</span><span class="o">.</span><span class="n">enter</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">print_time</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="s1">&#39;keyword&#39;</span><span class="p">})</span>
 
132
<span class="gp">... </span>    <span class="n">s</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
 
133
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
 
134
<span class="gp">...</span>
 
135
<span class="gp">&gt;&gt;&gt; </span><span class="n">print_some_times</span><span class="p">()</span>
 
136
<span class="go">930343690.257</span>
 
137
<span class="go">From print_time 930343695.274 positional</span>
 
138
<span class="go">From print_time 930343695.275 keyword</span>
 
139
<span class="go">From print_time 930343700.273 default</span>
 
140
<span class="go">930343700.276</span>
 
141
</pre></div>
 
142
</div>
 
143
<div class="section" id="scheduler-objects">
 
144
<span id="id1"></span><h2>17.6.1. Scheduler Objects<a class="headerlink" href="#scheduler-objects" title="Permalink to this headline">¶</a></h2>
 
145
<p><a class="reference internal" href="#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal"><span class="pre">scheduler</span></code></a> instances have the following methods and attributes:</p>
 
146
<dl class="method">
 
147
<dt id="sched.scheduler.enterabs">
 
148
<code class="descclassname">scheduler.</code><code class="descname">enterabs</code><span class="sig-paren">(</span><em>time</em>, <em>priority</em>, <em>action</em>, <em>argument=()</em>, <em>kwargs={}</em><span class="sig-paren">)</span><a class="headerlink" href="#sched.scheduler.enterabs" title="Permalink to this definition">¶</a></dt>
 
149
<dd><p>Schedule a new event. The <em>time</em> argument should be a numeric type compatible
 
150
with the return value of the <em>timefunc</em> function passed  to the constructor.
 
151
Events scheduled for the same <em>time</em> will be executed in the order of their
 
152
<em>priority</em>.</p>
 
153
<p>Executing the event means executing <code class="docutils literal"><span class="pre">action(*argument,</span> <span class="pre">**kwargs)</span></code>.
 
154
<em>argument</em> is a sequence holding the positional arguments for <em>action</em>.
 
155
<em>kwargs</em> is a dictionary holding the keyword arguments for <em>action</em>.</p>
 
156
<p>Return value is an event which may be used for later cancellation of the event
 
157
(see <a class="reference internal" href="#sched.scheduler.cancel" title="sched.scheduler.cancel"><code class="xref py py-meth docutils literal"><span class="pre">cancel()</span></code></a>).</p>
 
158
<div class="versionchanged">
 
159
<p><span class="versionmodified">Changed in version 3.3: </span><em>argument</em> parameter is optional.</p>
 
160
</div>
 
161
<div class="versionadded">
 
162
<p><span class="versionmodified">New in version 3.3: </span><em>kwargs</em> parameter was added.</p>
 
163
</div>
 
164
</dd></dl>
 
165
 
 
166
<dl class="method">
 
167
<dt id="sched.scheduler.enter">
 
168
<code class="descclassname">scheduler.</code><code class="descname">enter</code><span class="sig-paren">(</span><em>delay</em>, <em>priority</em>, <em>action</em>, <em>argument=()</em>, <em>kwargs={}</em><span class="sig-paren">)</span><a class="headerlink" href="#sched.scheduler.enter" title="Permalink to this definition">¶</a></dt>
 
169
<dd><p>Schedule an event for <em>delay</em> more time units. Other than the relative time, the
 
170
other arguments, the effect and the return value are the same as those for
 
171
<a class="reference internal" href="#sched.scheduler.enterabs" title="sched.scheduler.enterabs"><code class="xref py py-meth docutils literal"><span class="pre">enterabs()</span></code></a>.</p>
 
172
<div class="versionchanged">
 
173
<p><span class="versionmodified">Changed in version 3.3: </span><em>argument</em> parameter is optional.</p>
 
174
</div>
 
175
<div class="versionadded">
 
176
<p><span class="versionmodified">New in version 3.3: </span><em>kwargs</em> parameter was added.</p>
 
177
</div>
 
178
</dd></dl>
 
179
 
 
180
<dl class="method">
 
181
<dt id="sched.scheduler.cancel">
 
182
<code class="descclassname">scheduler.</code><code class="descname">cancel</code><span class="sig-paren">(</span><em>event</em><span class="sig-paren">)</span><a class="headerlink" href="#sched.scheduler.cancel" title="Permalink to this definition">¶</a></dt>
 
183
<dd><p>Remove the event from the queue. If <em>event</em> is not an event currently in the
 
184
queue, this method will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a>.</p>
 
185
</dd></dl>
 
186
 
 
187
<dl class="method">
 
188
<dt id="sched.scheduler.empty">
 
189
<code class="descclassname">scheduler.</code><code class="descname">empty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sched.scheduler.empty" title="Permalink to this definition">¶</a></dt>
 
190
<dd><p>Return true if the event queue is empty.</p>
 
191
</dd></dl>
 
192
 
 
193
<dl class="method">
 
194
<dt id="sched.scheduler.run">
 
195
<code class="descclassname">scheduler.</code><code class="descname">run</code><span class="sig-paren">(</span><em>blocking=True</em><span class="sig-paren">)</span><a class="headerlink" href="#sched.scheduler.run" title="Permalink to this definition">¶</a></dt>
 
196
<dd><p>Run all scheduled events. This method will wait  (using the <code class="xref py py-func docutils literal"><span class="pre">delayfunc()</span></code>
 
197
function passed to the constructor) for the next event, then execute it and so
 
198
on until there are no more scheduled events.</p>
 
199
<p>If <em>blocking</em> is false executes the scheduled events due to expire soonest
 
200
(if any) and then return the deadline of the next scheduled call in the
 
201
scheduler (if any).</p>
 
202
<p>Either <em>action</em> or <em>delayfunc</em> can raise an exception.  In either case, the
 
203
scheduler will maintain a consistent state and propagate the exception.  If an
 
204
exception is raised by <em>action</em>, the event will not be attempted in future calls
 
205
to <a class="reference internal" href="#sched.scheduler.run" title="sched.scheduler.run"><code class="xref py py-meth docutils literal"><span class="pre">run()</span></code></a>.</p>
 
206
<p>If a sequence of events takes longer to run than the time available before the
 
207
next event, the scheduler will simply fall behind.  No events will be dropped;
 
208
the calling code is responsible for canceling  events which are no longer
 
209
pertinent.</p>
 
210
<div class="versionadded">
 
211
<p><span class="versionmodified">New in version 3.3: </span><em>blocking</em> parameter was added.</p>
 
212
</div>
 
213
</dd></dl>
 
214
 
 
215
<dl class="attribute">
 
216
<dt id="sched.scheduler.queue">
 
217
<code class="descclassname">scheduler.</code><code class="descname">queue</code><a class="headerlink" href="#sched.scheduler.queue" title="Permalink to this definition">¶</a></dt>
 
218
<dd><p>Read-only attribute returning a list of upcoming events in the order they
 
219
will be run.  Each event is shown as a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> with the
 
220
following fields:  time, priority, action, argument, kwargs.</p>
 
221
</dd></dl>
 
222
 
 
223
</div>
 
224
</div>
 
225
 
 
226
 
 
227
          </div>
 
228
        </div>
 
229
      </div>
 
230
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
231
        <div class="sphinxsidebarwrapper">
 
232
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
233
  <ul>
 
234
<li><a class="reference internal" href="#">17.6. <code class="docutils literal"><span class="pre">sched</span></code> &#8212; Event scheduler</a><ul>
 
235
<li><a class="reference internal" href="#scheduler-objects">17.6.1. Scheduler Objects</a></li>
 
236
</ul>
 
237
</li>
 
238
</ul>
 
239
 
 
240
  <h4>Previous topic</h4>
 
241
  <p class="topless"><a href="subprocess.html"
 
242
                        title="previous chapter">17.5. <code class="docutils literal"><span class="pre">subprocess</span></code> &#8212; Subprocess management</a></p>
 
243
  <h4>Next topic</h4>
 
244
  <p class="topless"><a href="queue.html"
 
245
                        title="next chapter">17.7. <code class="docutils literal"><span class="pre">queue</span></code> &#8212; A synchronized queue class</a></p>
 
246
  <div role="note" aria-label="source link">
 
247
    <h3>This Page</h3>
 
248
    <ul class="this-page-menu">
 
249
      <li><a href="../bugs.html">Report a Bug</a></li>
 
250
      <li><a href="../_sources/library/sched.txt"
 
251
            rel="nofollow">Show Source</a></li>
 
252
    </ul>
 
253
  </div>
 
254
        </div>
 
255
      </div>
 
256
      <div class="clearer"></div>
 
257
    </div>  
 
258
    <div class="related" role="navigation" aria-label="related navigation">
 
259
      <h3>Navigation</h3>
 
260
      <ul>
 
261
        <li class="right" style="margin-right: 10px">
 
262
          <a href="../genindex.html" title="General Index"
 
263
             >index</a></li>
 
264
        <li class="right" >
 
265
          <a href="../py-modindex.html" title="Python Module Index"
 
266
             >modules</a> |</li>
 
267
        <li class="right" >
 
268
          <a href="queue.html" title="17.7. queue — A synchronized queue class"
 
269
             >next</a> |</li>
 
270
        <li class="right" >
 
271
          <a href="subprocess.html" title="17.5. subprocess — Subprocess management"
 
272
             >previous</a> |</li>
 
273
        <li><img src="../_static/py.png" alt=""
 
274
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
275
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
276
        <li>
 
277
          <span class="version_switcher_placeholder">3.5.2</span>
 
278
          <a href="../index.html">Documentation </a> &raquo;
 
279
        </li>
 
280
 
 
281
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
282
          <li class="nav-item nav-item-2"><a href="concurrency.html" >17. Concurrent Execution</a> &raquo;</li>
 
283
    <li class="right">
 
284
        
 
285
 
 
286
    <div class="inline-search" style="display: none" role="search">
 
287
        <form class="inline-search" action="../search.html" method="get">
 
288
          <input placeholder="Quick search" type="text" name="q" />
 
289
          <input type="submit" value="Go" />
 
290
          <input type="hidden" name="check_keywords" value="yes" />
 
291
          <input type="hidden" name="area" value="default" />
 
292
        </form>
 
293
    </div>
 
294
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
295
         |
 
296
    </li>
 
297
 
 
298
      </ul>
 
299
    </div>  
 
300
    <div class="footer">
 
301
    &copy; <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
 
302
    <br />
 
303
    The Python Software Foundation is a non-profit corporation.
 
304
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
305
    <br />
 
306
    Last updated on Sep 23, 2016.
 
307
    <a href="../bugs.html">Found a bug</a>?
 
308
    <br />
 
309
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
310
    </div>
 
311
 
 
312
  </body>
 
313
</html>
 
 
b'\\ No newline at end of file'