Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-release > by-pkgid > db93d7191b12a3d5ce887da46fc79bf1 > files > 205

python-twisted-core-doc-2.5.0-3mdv2008.1.x86_64.rpm

<?xml version="1.0"?><!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" lang="en"><head><title>Twisted Documentation: Scheduling tasks for the future</title><link href="../howto/stylesheet.css" type="text/css" rel="stylesheet" /></head><body bgcolor="white"><h1 class="title">Scheduling tasks for the future</h1><div class="toc"><ol></ol></div><div class="content"><span></span><p>Let's say we want to run a task X seconds in the future.
    The way to do that is defined in the reactor interface <code class="API">twisted.internet.interfaces.IReactorTime</code>:</p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">f</span>(<span class="py-src-parameter">s</span>):
    <span class="py-src-keyword">print</span> <span class="py-src-string">&quot;this will run 3.5 seconds after it was scheduled: %s&quot;</span> % <span class="py-src-variable">s</span>

<span class="py-src-variable">reactor</span>.<span class="py-src-variable">callLater</span>(<span class="py-src-number">3.5</span>, <span class="py-src-variable">f</span>, <span class="py-src-string">&quot;hello, world&quot;</span>)

<span class="py-src-comment"># f() will only be called if the event loop was started:
</span><span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre><p>If we want a task to run every X seconds repeatedly, we can
    use <code class="API">twisted.internet.task.LoopingCall</code>:</p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">task</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">runEverySecond</span>():
    <span class="py-src-keyword">print</span> <span class="py-src-string">&quot;a second has passed&quot;</span>

<span class="py-src-variable">l</span> = <span class="py-src-variable">task</span>.<span class="py-src-variable">LoopingCall</span>(<span class="py-src-variable">runEverySecond</span>)
<span class="py-src-variable">l</span>.<span class="py-src-variable">start</span>(<span class="py-src-number">1.0</span>) <span class="py-src-comment"># call every second</span>

<span class="py-src-comment"># l.stop() will stop the looping calls
</span><span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre><p>If we want to cancel a task that we've scheduled:</p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>

<span class="py-src-keyword">def</span> <span class="py-src-identifier">f</span>():
    <span class="py-src-keyword">print</span> <span class="py-src-string">&quot;I'll never run.&quot;</span>

<span class="py-src-variable">callID</span> = <span class="py-src-variable">reactor</span>.<span class="py-src-variable">callLater</span>(<span class="py-src-number">5</span>, <span class="py-src-variable">f</span>)
<span class="py-src-variable">callID</span>.<span class="py-src-variable">cancel</span>()
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre><p>As with all reactor-based code, in order for scheduling to work the reactor must be started using <code class="python">reactor.run()</code>.</p></div><p><a href="../howto/index.html">Index</a></p><span class="version">Version: 2.5.0</span></body></html>