<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8.8. sched — Event scheduler — Python v2.6.6 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="top" title="Python v2.6.6 documentation" href="../index.html" />
<link rel="up" title="8. Data Types" href="datatypes.html" />
<link rel="next" title="8.9. mutex — Mutual exclusion support" href="mutex.html" />
<link rel="prev" title="8.7. sets — Unordered collections of unique elements" href="sets.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
</head>
<body>
<div class="related">
<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="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="mutex.html" title="8.9. mutex — Mutual exclusion support"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="sets.html" title="8.7. sets — Unordered collections of unique elements"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-sched">
<h1>8.8. <tt class="xref docutils literal"><span class="pre">sched</span></tt> — Event scheduler<a class="headerlink" href="#module-sched" title="Permalink to this headline">¶</a></h1>
<p id="index-541">The <tt class="xref docutils literal"><span class="pre">sched</span></tt> module defines a class which implements a general purpose event
scheduler:</p>
<dl class="class">
<dt id="sched.scheduler">
<em class="property">class </em><tt class="descclassname">sched.</tt><tt class="descname">scheduler</tt><big>(</big><em>timefunc</em>, <em>delayfunc</em><big>)</big><a class="headerlink" href="#sched.scheduler" title="Permalink to this definition">¶</a></dt>
<dd>The <a title="sched.scheduler" class="reference internal" href="#sched.scheduler"><tt class="xref docutils literal"><span class="pre">scheduler</span></tt></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 <tt class="docutils literal"><span class="pre">0</span></tt> after each
event is run to allow other threads an opportunity to run in multi-threaded
applications.</dd></dl>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><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="k">print</span> <span class="s">"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="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="k">print</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">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="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="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="k">print</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</span>
<span class="go">From print_time 930343700.273</span>
<span class="go">930343700.276</span>
</pre></div>
</div>
<p>In multi-threaded environments, the <a title="sched.scheduler" class="reference internal" href="#sched.scheduler"><tt class="xref docutils literal"><span class="pre">scheduler</span></tt></a> class has limitations
with respect to thread-safety, inability to insert a new task before
the one currently pending in a running scheduler, and holding up the main
thread until the event queue is empty. Instead, the preferred approach
is to use the <a title="threading.Timer" class="reference external" href="threading.html#threading.Timer"><tt class="xref docutils literal"><span class="pre">threading.Timer</span></tt></a> class instead.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">time</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">Timer</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">print_time</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">print</span> <span class="s">"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="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="k">print</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">Timer</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">print_time</span><span class="p">,</span> <span class="p">())</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">Timer</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">print_time</span><span class="p">,</span> <span class="p">())</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span> <span class="c"># sleep while time-delay events execute</span>
<span class="gp">... </span> <span class="k">print</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</span>
<span class="go">From print_time 930343700.273</span>
<span class="go">930343701.301</span>
</pre></div>
</div>
<div class="section" id="scheduler-objects">
<span id="id1"></span><h2>8.8.1. Scheduler Objects<a class="headerlink" href="#scheduler-objects" title="Permalink to this headline">¶</a></h2>
<p><a title="sched.scheduler" class="reference internal" href="#sched.scheduler"><tt class="xref docutils literal"><span class="pre">scheduler</span></tt></a> instances have the following methods and attributes:</p>
<dl class="method">
<dt id="sched.scheduler.enterabs">
<tt class="descclassname">scheduler.</tt><tt class="descname">enterabs</tt><big>(</big><em>time</em>, <em>priority</em>, <em>action</em>, <em>argument</em><big>)</big><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>.</p>
<p>Executing the event means executing <tt class="docutils literal"><span class="pre">action(*argument)</span></tt>. <em>argument</em> must be a
sequence holding the parameters for <em>action</em>.</p>
<p>Return value is an event which may be used for later cancellation of the event
(see <a title="sched.scheduler.cancel" class="reference internal" href="#sched.scheduler.cancel"><tt class="xref docutils literal"><span class="pre">cancel()</span></tt></a>).</p>
</dd></dl>
<dl class="method">
<dt id="sched.scheduler.enter">
<tt class="descclassname">scheduler.</tt><tt class="descname">enter</tt><big>(</big><em>delay</em>, <em>priority</em>, <em>action</em>, <em>argument</em><big>)</big><a class="headerlink" href="#sched.scheduler.enter" title="Permalink to this definition">¶</a></dt>
<dd>Schedule an event for <em>delay</em> more time units. Other then the relative time, the
other arguments, the effect and the return value are the same as those for
<a title="sched.scheduler.enterabs" class="reference internal" href="#sched.scheduler.enterabs"><tt class="xref docutils literal"><span class="pre">enterabs()</span></tt></a>.</dd></dl>
<dl class="method">
<dt id="sched.scheduler.cancel">
<tt class="descclassname">scheduler.</tt><tt class="descname">cancel</tt><big>(</big><em>event</em><big>)</big><a class="headerlink" href="#sched.scheduler.cancel" title="Permalink to this definition">¶</a></dt>
<dd>Remove the event from the queue. If <em>event</em> is not an event currently in the
queue, this method will raise a <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>.</dd></dl>
<dl class="method">
<dt id="sched.scheduler.empty">
<tt class="descclassname">scheduler.</tt><tt class="descname">empty</tt><big>(</big><big>)</big><a class="headerlink" href="#sched.scheduler.empty" title="Permalink to this definition">¶</a></dt>
<dd>Return true if the event queue is empty.</dd></dl>
<dl class="method">
<dt id="sched.scheduler.run">
<tt class="descclassname">scheduler.</tt><tt class="descname">run</tt><big>(</big><big>)</big><a class="headerlink" href="#sched.scheduler.run" title="Permalink to this definition">¶</a></dt>
<dd><p>Run all scheduled events. This function will wait (using the <tt class="xref docutils literal"><span class="pre">delayfunc()</span></tt>
function passed to the constructor) for the next event, then execute it and so
on until there are no more scheduled events.</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 title="sched.scheduler.run" class="reference internal" href="#sched.scheduler.run"><tt class="xref docutils literal"><span class="pre">run()</span></tt></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>
</dd></dl>
<dl class="attribute">
<dt id="sched.scheduler.queue">
<tt class="descclassname">scheduler.</tt><tt class="descname">queue</tt><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 external" href="../glossary.html#term-named-tuple"><em class="xref">named tuple</em></a> with the
following fields: time, priority, action, argument.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference external" href="#">8.8. <tt class="docutils literal"><span class="pre">sched</span></tt> — Event scheduler</a><ul>
<li><a class="reference external" href="#scheduler-objects">8.8.1. Scheduler Objects</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="sets.html"
title="previous chapter">8.7. <tt class="docutils literal docutils literal"><span class="pre">sets</span></tt> — Unordered collections of unique elements</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="mutex.html"
title="next chapter">8.9. <tt class="docutils literal docutils literal docutils literal"><span class="pre">mutex</span></tt> — Mutual exclusion support</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li><a href="../_sources/library/sched.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<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="../modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="mutex.html" title="8.9. mutex — Mutual exclusion support"
>next</a> |</li>
<li class="right" >
<a href="sets.html" title="8.7. sets — Unordered collections of unique elements"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="datatypes.html" >8. Data Types</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="http://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on Jul 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez