318 lines
20 KiB
HTML
318 lines
20 KiB
HTML
|
|
<!DOCTYPE html>
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>sched — Event scheduler — Python 3.7.4 documentation</title>
|
|
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|
<script type="text/javascript" src="../_static/language_data.js"></script>
|
|
|
|
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
|
|
|
<link rel="search" type="application/opensearchdescription+xml"
|
|
title="Search within Python 3.7.4 documentation"
|
|
href="../_static/opensearch.xml"/>
|
|
<link rel="author" title="About these documents" href="../about.html" />
|
|
<link rel="index" title="Index" href="../genindex.html" />
|
|
<link rel="search" title="Search" href="../search.html" />
|
|
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
|
<link rel="next" title="queue — A synchronized queue class" href="queue.html" />
|
|
<link rel="prev" title="subprocess — Subprocess management" href="subprocess.html" />
|
|
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
|
<link rel="canonical" href="https://docs.python.org/3/library/sched.html" />
|
|
|
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|
<script type="text/javascript" src="../_static/switchers.js"></script>
|
|
|
|
|
|
|
|
<style>
|
|
@media only screen {
|
|
table.full-width-table {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
|
|
</head><body>
|
|
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="queue.html" title="queue — A synchronized queue class"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="subprocess.html" title="subprocess — Subprocess management"
|
|
accesskey="P">previous</a> |</li>
|
|
<li><img src="../_static/py.png" alt=""
|
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li>
|
|
<span class="language_switcher_placeholder">en</span>
|
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|
<a href="../index.html">Documentation </a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">Concurrent Execution</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" style="display: none" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="Quick search" type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
<script type="text/javascript">$('.inline-search').show(0);</script>
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<div class="section" id="module-sched">
|
|
<span id="sched-event-scheduler"></span><h1><a class="reference internal" href="#module-sched" title="sched: General purpose event scheduler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code></a> — Event scheduler<a class="headerlink" href="#module-sched" title="Permalink to this headline">¶</a></h1>
|
|
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/sched.py">Lib/sched.py</a></p>
|
|
<hr class="docutils" id="index-0" />
|
|
<p>The <a class="reference internal" href="#module-sched" title="sched: General purpose event scheduler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code></a> module defines a class which implements a general purpose event
|
|
scheduler:</p>
|
|
<dl class="class">
|
|
<dt id="sched.scheduler">
|
|
<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>
|
|
<dd><p>The <a class="reference internal" href="#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal notranslate"><span class="pre">scheduler</span></code></a> class defines a generic interface to scheduling events.
|
|
It needs two functions to actually deal with the “outside world” — <em>timefunc</em>
|
|
should be callable without arguments, and return a number (the “time”, in any
|
|
units whatsoever). The <em>delayfunc</em> function should be callable with one
|
|
argument, compatible with the output of <em>timefunc</em>, and should delay that many
|
|
time units. <em>delayfunc</em> will also be called with the argument <code class="docutils literal notranslate"><span class="pre">0</span></code> after each
|
|
event is run to allow other threads an opportunity to run in multi-threaded
|
|
applications.</p>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.3: </span><em>timefunc</em> and <em>delayfunc</em> parameters are optional.</p>
|
|
</div>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal notranslate"><span class="pre">scheduler</span></code></a> class can be safely used in multi-threaded
|
|
environments.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<p>Example:</p>
|
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sched</span><span class="o">,</span> <span class="nn">time</span>
|
|
<span class="gp">>>> </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>
|
|
<span class="gp">>>> </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">'default'</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"From print_time"</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>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">print_some_times</span><span class="p">():</span>
|
|
<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>
|
|
<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>
|
|
<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">'positional'</span><span class="p">,))</span>
|
|
<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">'a'</span><span class="p">:</span> <span class="s1">'keyword'</span><span class="p">})</span>
|
|
<span class="gp">... </span> <span class="n">s</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
|
|
<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>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">print_some_times</span><span class="p">()</span>
|
|
<span class="go">930343690.257</span>
|
|
<span class="go">From print_time 930343695.274 positional</span>
|
|
<span class="go">From print_time 930343695.275 keyword</span>
|
|
<span class="go">From print_time 930343700.273 default</span>
|
|
<span class="go">930343700.276</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="section" id="scheduler-objects">
|
|
<span id="id1"></span><h2>Scheduler Objects<a class="headerlink" href="#scheduler-objects" title="Permalink to this headline">¶</a></h2>
|
|
<p><a class="reference internal" href="#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal notranslate"><span class="pre">scheduler</span></code></a> instances have the following methods and attributes:</p>
|
|
<dl class="method">
|
|
<dt id="sched.scheduler.enterabs">
|
|
<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>
|
|
<dd><p>Schedule a new event. The <em>time</em> argument should be a numeric type compatible
|
|
with the return value of the <em>timefunc</em> function passed to the constructor.
|
|
Events scheduled for the same <em>time</em> will be executed in the order of their
|
|
<em>priority</em>. A lower number represents a higher priority.</p>
|
|
<p>Executing the event means executing <code class="docutils literal notranslate"><span class="pre">action(*argument,</span> <span class="pre">**kwargs)</span></code>.
|
|
<em>argument</em> is a sequence holding the positional arguments for <em>action</em>.
|
|
<em>kwargs</em> is a dictionary holding the keyword arguments for <em>action</em>.</p>
|
|
<p>Return value is an event which may be used for later cancellation of the event
|
|
(see <a class="reference internal" href="#sched.scheduler.cancel" title="sched.scheduler.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cancel()</span></code></a>).</p>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.3: </span><em>argument</em> parameter is optional.</p>
|
|
</div>
|
|
<div class="versionadded">
|
|
<p><span class="versionmodified added">New in version 3.3: </span><em>kwargs</em> parameter was added.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="sched.scheduler.enter">
|
|
<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>
|
|
<dd><p>Schedule an event for <em>delay</em> more time units. Other than the relative time, the
|
|
other arguments, the effect and the return value are the same as those for
|
|
<a class="reference internal" href="#sched.scheduler.enterabs" title="sched.scheduler.enterabs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enterabs()</span></code></a>.</p>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.3: </span><em>argument</em> parameter is optional.</p>
|
|
</div>
|
|
<div class="versionadded">
|
|
<p><span class="versionmodified added">New in version 3.3: </span><em>kwargs</em> parameter was added.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="sched.scheduler.cancel">
|
|
<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>
|
|
<dd><p>Remove the event from the queue. If <em>event</em> is not an event currently in the
|
|
queue, this method will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="sched.scheduler.empty">
|
|
<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>
|
|
<dd><p>Return true if the event queue is empty.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="sched.scheduler.run">
|
|
<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>
|
|
<dd><p>Run all scheduled events. This method will wait (using the <code class="xref py py-func docutils literal notranslate"><span class="pre">delayfunc()</span></code>
|
|
function passed to the constructor) for the next event, then execute it and so
|
|
on until there are no more scheduled events.</p>
|
|
<p>If <em>blocking</em> is false executes the scheduled events due to expire soonest
|
|
(if any) and then return the deadline of the next scheduled call in the
|
|
scheduler (if any).</p>
|
|
<p>Either <em>action</em> or <em>delayfunc</em> can raise an exception. In either case, the
|
|
scheduler will maintain a consistent state and propagate the exception. If an
|
|
exception is raised by <em>action</em>, the event will not be attempted in future calls
|
|
to <a class="reference internal" href="#sched.scheduler.run" title="sched.scheduler.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>.</p>
|
|
<p>If a sequence of events takes longer to run than the time available before the
|
|
next event, the scheduler will simply fall behind. No events will be dropped;
|
|
the calling code is responsible for canceling events which are no longer
|
|
pertinent.</p>
|
|
<div class="versionadded">
|
|
<p><span class="versionmodified added">New in version 3.3: </span><em>blocking</em> parameter was added.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="sched.scheduler.queue">
|
|
<code class="descclassname">scheduler.</code><code class="descname">queue</code><a class="headerlink" href="#sched.scheduler.queue" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Read-only attribute returning a list of upcoming events in the order they
|
|
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
|
|
following fields: time, priority, action, argument, kwargs.</p>
|
|
</dd></dl>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="../contents.html">Table of Contents</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> — Event scheduler</a><ul>
|
|
<li><a class="reference internal" href="#scheduler-objects">Scheduler Objects</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="subprocess.html"
|
|
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code> — Subprocess management</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="queue.html"
|
|
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code> — A synchronized queue class</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">Report a Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/sched.rst"
|
|
rel="nofollow">Show Source
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="queue.html" title="queue — A synchronized queue class"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="subprocess.html" title="subprocess — Subprocess management"
|
|
>previous</a> |</li>
|
|
<li><img src="../_static/py.png" alt=""
|
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li>
|
|
<span class="language_switcher_placeholder">en</span>
|
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|
<a href="../index.html">Documentation </a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="concurrency.html" >Concurrent Execution</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" style="display: none" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="Quick search" type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
<script type="text/javascript">$('.inline-search').show(0);</script>
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
|
|
<br />
|
|
The Python Software Foundation is a non-profit corporation.
|
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|
<br />
|
|
Last updated on Jul 13, 2019.
|
|
<a href="../bugs.html">Found a bug</a>?
|
|
<br />
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
|
</div>
|
|
|
|
</body>
|
|
</html> |