Python Library Task
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.html 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <!DOCTYPE html>
  2. <html class="writer-html4" lang="en" >
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Welcome to task’s documentation! &mdash; task documentation</title>
  7. <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <!--[if lt IE 9]>
  10. <script src="_static/js/html5shiv.min.js"></script>
  11. <![endif]-->
  12. <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
  13. <script type="text/javascript" src="_static/jquery.js"></script>
  14. <script type="text/javascript" src="_static/underscore.js"></script>
  15. <script type="text/javascript" src="_static/doctools.js"></script>
  16. <script type="text/javascript" src="_static/language_data.js"></script>
  17. <script type="text/javascript" src="_static/js/theme.js"></script>
  18. <link rel="index" title="Index" href="genindex.html" />
  19. <link rel="search" title="Search" href="search.html" />
  20. </head>
  21. <body class="wy-body-for-nav">
  22. <div class="wy-grid-for-nav">
  23. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  24. <div class="wy-side-scroll">
  25. <div class="wy-side-nav-search" >
  26. <a href="#" class="icon icon-home" alt="Documentation Home"> task
  27. </a>
  28. <div role="search">
  29. <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
  30. <input type="text" name="q" placeholder="Search docs" />
  31. <input type="hidden" name="check_keywords" value="yes" />
  32. <input type="hidden" name="area" value="default" />
  33. </form>
  34. </div>
  35. </div>
  36. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  37. <!-- Local TOC -->
  38. <div class="local-toc"><ul>
  39. <li><a class="reference internal" href="#">Welcome to task’s documentation!</a><ul>
  40. <li><a class="reference internal" href="#task-task-module">task (Task Module)</a></li>
  41. </ul>
  42. </li>
  43. <li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
  44. </ul>
  45. </div>
  46. </div>
  47. </div>
  48. </nav>
  49. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  50. <nav class="wy-nav-top" aria-label="top navigation">
  51. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  52. <a href="#">task</a>
  53. </nav>
  54. <div class="wy-nav-content">
  55. <div class="rst-content">
  56. <div role="navigation" aria-label="breadcrumbs navigation">
  57. <ul class="wy-breadcrumbs">
  58. <li><a href="#" class="icon icon-home"></a> &raquo;</li>
  59. <li>Welcome to task’s documentation!</li>
  60. <li class="wy-breadcrumbs-aside">
  61. <a href="_sources/index.rst.txt" rel="nofollow"> View page source</a>
  62. </li>
  63. </ul>
  64. <hr/>
  65. </div>
  66. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  67. <div itemprop="articleBody">
  68. <div class="section" id="module-task">
  69. <span id="welcome-to-task-s-documentation"></span><h1>Welcome to task’s documentation!<a class="headerlink" href="#module-task" title="Permalink to this headline">¶</a></h1>
  70. <div class="section" id="task-task-module">
  71. <h2>task (Task Module)<a class="headerlink" href="#task-task-module" title="Permalink to this headline">¶</a></h2>
  72. <p><strong>Author:</strong></p>
  73. <ul class="simple">
  74. <li>Dirk Alders &lt;<a class="reference external" href="mailto:sudo-dirk&#37;&#52;&#48;mount-mockery&#46;de">sudo-dirk<span>&#64;</span>mount-mockery<span>&#46;</span>de</a>&gt;</li>
  75. </ul>
  76. <p><strong>Description:</strong></p>
  77. <blockquote>
  78. <div>This Module supports helpfull classes for queues, tasks, …</div></blockquote>
  79. <p><strong>Submodules:</strong></p>
  80. <ul class="simple">
  81. <li><a class="reference internal" href="#task.crontab" title="task.crontab"><code class="xref py py-class docutils literal notranslate"><span class="pre">task.crontab</span></code></a></li>
  82. <li><a class="reference internal" href="#task.delayed" title="task.delayed"><code class="xref py py-class docutils literal notranslate"><span class="pre">task.delayed</span></code></a></li>
  83. <li><a class="reference internal" href="#task.periodic" title="task.periodic"><code class="xref py py-class docutils literal notranslate"><span class="pre">task.periodic</span></code></a></li>
  84. <li><a class="reference internal" href="#task.queue" title="task.queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">task.queue</span></code></a></li>
  85. <li><a class="reference internal" href="#task.threaded_queue" title="task.threaded_queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">task.threaded_queue</span></code></a></li>
  86. </ul>
  87. <p><strong>Unittest:</strong></p>
  88. <blockquote>
  89. <div>See also the <a class="reference download internal" download="" href="_downloads/89eb74c94e7790229553fd55e6dfe3a3/unittest.pdf"><code class="xref download docutils literal notranslate"><span class="pre">unittest</span></code></a> documentation.</div></blockquote>
  90. <p><strong>Module Documentation:</strong></p>
  91. <dl class="class">
  92. <dt id="task.crontab">
  93. <em class="property">class </em><code class="descclassname">task.</code><code class="descname">crontab</code><span class="sig-paren">(</span><em>accuracy=30</em><span class="sig-paren">)</span><a class="headerlink" href="#task.crontab" title="Permalink to this definition">¶</a></dt>
  94. <dd><p>Class to execute a callback at the specified time conditions. See also parent <a class="reference internal" href="#task.periodic" title="task.periodic"><code class="xref py py-class docutils literal notranslate"><span class="pre">periodic</span></code></a>.</p>
  95. <table class="docutils field-list" frame="void" rules="none">
  96. <col class="field-name" />
  97. <col class="field-body" />
  98. <tbody valign="top">
  99. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>accuracy</strong> (<em>float</em>) – Repeat time in seconds for background task checking event triggering. This time is the maximum delay between specified time condition and the execution.</td>
  100. </tr>
  101. </tbody>
  102. </table>
  103. <p><strong>Example:</strong></p>
  104. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  105. <span class="c1"># -*- coding: UTF-8 -*-</span>
  106. <span class="kn">import</span> <span class="nn">sys</span>
  107. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  108. <span class="kn">import</span> <span class="nn">task</span>
  109. <span class="kn">import</span> <span class="nn">time</span>
  110. <span class="k">def</span> <span class="nf">print_localtime</span><span class="p">(</span><span class="n">cj</span><span class="p">):</span>
  111. <span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">())</span>
  112. <span class="n">ct</span> <span class="o">=</span> <span class="n">task</span><span class="o">.</span><span class="n">crontab</span><span class="p">(</span><span class="n">accuracy</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span>
  113. <span class="n">minute</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%M&#39;</span><span class="p">))</span>
  114. <span class="n">ct</span><span class="o">.</span><span class="n">add_cronjob</span><span class="p">([</span><span class="n">minute</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">minute</span> <span class="o">+</span> <span class="mi">3</span><span class="p">],</span> <span class="n">task</span><span class="o">.</span><span class="n">crontab</span><span class="o">.</span><span class="n">ANY</span><span class="p">,</span> <span class="n">task</span><span class="o">.</span><span class="n">crontab</span><span class="o">.</span><span class="n">ANY</span><span class="p">,</span> <span class="n">task</span><span class="o">.</span><span class="n">crontab</span><span class="o">.</span><span class="n">ANY</span><span class="p">,</span> <span class="n">task</span><span class="o">.</span><span class="n">crontab</span><span class="o">.</span><span class="n">ANY</span><span class="p">,</span> <span class="n">print_localtime</span><span class="p">)</span>
  115. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Cronjob added for Minute: </span><span class="si">%02d</span><span class="s1">, </span><span class="si">%02d</span><span class="se">\n</span><span class="s1">--------------------------------</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">minute</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">minute</span> <span class="o">+</span> <span class="mi">3</span><span class="p">))</span>
  116. <span class="n">ct</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  117. <span class="k">try</span><span class="p">:</span>
  118. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">195</span><span class="p">)</span>
  119. <span class="n">ct</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  120. <span class="n">ct</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  121. <span class="k">finally</span><span class="p">:</span>
  122. <span class="n">ct</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  123. </pre></div>
  124. </div>
  125. <p>Will result to the following output:</p>
  126. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Cronjob</span> <span class="n">added</span> <span class="k">for</span> <span class="n">Minute</span><span class="p">:</span> <span class="mi">45</span><span class="p">,</span> <span class="mi">47</span>
  127. <span class="o">--------------------------------</span>
  128. <span class="n">time</span><span class="o">.</span><span class="n">struct_time</span><span class="p">(</span><span class="n">tm_year</span><span class="o">=</span><span class="mi">2021</span><span class="p">,</span> <span class="n">tm_mon</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">tm_mday</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">tm_hour</span><span class="o">=</span><span class="mi">17</span><span class="p">,</span> <span class="n">tm_min</span><span class="o">=</span><span class="mi">45</span><span class="p">,</span> <span class="n">tm_sec</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">tm_wday</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">tm_yday</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">tm_isdst</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
  129. <span class="n">time</span><span class="o">.</span><span class="n">struct_time</span><span class="p">(</span><span class="n">tm_year</span><span class="o">=</span><span class="mi">2021</span><span class="p">,</span> <span class="n">tm_mon</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">tm_mday</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">tm_hour</span><span class="o">=</span><span class="mi">17</span><span class="p">,</span> <span class="n">tm_min</span><span class="o">=</span><span class="mi">47</span><span class="p">,</span> <span class="n">tm_sec</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">tm_wday</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">tm_yday</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">tm_isdst</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
  130. </pre></div>
  131. </div>
  132. <dl class="attribute">
  133. <dt id="task.crontab.ANY">
  134. <code class="descname">ANY</code><em class="property"> = '*'</em><a class="headerlink" href="#task.crontab.ANY" title="Permalink to this definition">¶</a></dt>
  135. <dd><p>Constant for matching every condition.</p>
  136. </dd></dl>
  137. <dl class="method">
  138. <dt id="task.crontab.add_cronjob">
  139. <code class="descname">add_cronjob</code><span class="sig-paren">(</span><em>minute</em>, <em>hour</em>, <em>day_of_month</em>, <em>month</em>, <em>day_of_week</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#task.crontab.add_cronjob" title="Permalink to this definition">¶</a></dt>
  140. <dd><p>This Method adds a cronjob to be executed.</p>
  141. <table class="docutils field-list" frame="void" rules="none">
  142. <col class="field-name" />
  143. <col class="field-body" />
  144. <tbody valign="top">
  145. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  146. <li><strong>minute</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Minute for execution. Either 0…59, [0…59, 0…59, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Minute.</li>
  147. <li><strong>hour</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Hour for execution. Either 0…23, [0…23, 0…23, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Hour.</li>
  148. <li><strong>day_of_month</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Day of Month for execution. Either 0…31, [0…31, 0…31, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Day of Month.</li>
  149. <li><strong>month</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Month for execution. Either 0…12, [0…12, 0…12, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Month.</li>
  150. <li><strong>day_of_week</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Day of Week for execution. Either 0…6, [0…6, 0…6, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Day of Week.</li>
  151. <li><strong>callback</strong> (<em>func</em>) – The callback to be executed. The instance of <a class="reference internal" href="#task.crontab.cronjob" title="task.crontab.cronjob"><code class="xref py py-class docutils literal notranslate"><span class="pre">cronjob</span></code></a> will be given as the first, args and kwargs as the following parameters.</li>
  152. </ul>
  153. </td>
  154. </tr>
  155. </tbody>
  156. </table>
  157. <div class="admonition note">
  158. <p class="first admonition-title">Note</p>
  159. <p class="last">The <code class="docutils literal notranslate"><span class="pre">callback</span></code> will be executed with it’s instance of <a class="reference internal" href="#task.crontab.cronjob" title="task.crontab.cronjob"><code class="xref py py-class docutils literal notranslate"><span class="pre">cronjob</span></code></a> as the first parameter.
  160. The given Arguments (<code class="xref py py-data docutils literal notranslate"><span class="pre">args</span></code>) and keyword Arguments (<code class="xref py py-data docutils literal notranslate"><span class="pre">kwargs</span></code>) will be stored in that object.</p>
  161. </div>
  162. </dd></dl>
  163. <dl class="class">
  164. <dt id="task.crontab.cronjob">
  165. <em class="property">class </em><code class="descname">cronjob</code><span class="sig-paren">(</span><em>minute</em>, <em>hour</em>, <em>day_of_month</em>, <em>month</em>, <em>day_of_week</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#task.crontab.cronjob" title="Permalink to this definition">¶</a></dt>
  166. <dd><p>Class to handle cronjob parameters and cronjob changes.</p>
  167. <table class="docutils field-list" frame="void" rules="none">
  168. <col class="field-name" />
  169. <col class="field-body" />
  170. <tbody valign="top">
  171. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  172. <li><strong>minute</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Minute for execution. Either 0…59, [0…59, 0…59, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Minute.</li>
  173. <li><strong>hour</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Hour for execution. Either 0…23, [0…23, 0…23, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Hour.</li>
  174. <li><strong>day_of_month</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Day of Month for execution. Either 0…31, [0…31, 0…31, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Day of Month.</li>
  175. <li><strong>month</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Month for execution. Either 0…12, [0…12, 0…12, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Month.</li>
  176. <li><strong>day_of_week</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Day of Week for execution. Either 0…6, [0…6, 0…6, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Day of Week.</li>
  177. <li><strong>callback</strong> (<em>func</em>) – The callback to be executed. The instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">cronjob</span></code> will be given as the first, args and kwargs as the following parameters.</li>
  178. </ul>
  179. </td>
  180. </tr>
  181. </tbody>
  182. </table>
  183. <div class="admonition note">
  184. <p class="first admonition-title">Note</p>
  185. <p class="last">This class should not be used stand alone. An instance will be created by adding a cronjob by using <a class="reference internal" href="#task.crontab.add_cronjob" title="task.crontab.add_cronjob"><code class="xref py py-func docutils literal notranslate"><span class="pre">crontab.add_cronjob()</span></code></a>.</p>
  186. </div>
  187. <dl class="class">
  188. <dt id="task.crontab.cronjob.all_match">
  189. <em class="property">class </em><code class="descname">all_match</code><a class="headerlink" href="#task.crontab.cronjob.all_match" title="Permalink to this definition">¶</a></dt>
  190. <dd><p>Universal set - match everything</p>
  191. </dd></dl>
  192. <dl class="method">
  193. <dt id="task.crontab.cronjob.cron_execution">
  194. <code class="descname">cron_execution</code><span class="sig-paren">(</span><em>tm</em><span class="sig-paren">)</span><a class="headerlink" href="#task.crontab.cronjob.cron_execution" title="Permalink to this definition">¶</a></dt>
  195. <dd><p>This Methods executes the Cron-Callback, if a execution is needed for the given time (depending on the parameters on initialisation)</p>
  196. <table class="docutils field-list" frame="void" rules="none">
  197. <col class="field-name" />
  198. <col class="field-body" />
  199. <tbody valign="top">
  200. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>tm</strong> (<em>int</em>) – (Current) Time Value to be checked. The time needs to be given in seconds since 1970 (e.g. generated by int(time.time())).</td>
  201. </tr>
  202. </tbody>
  203. </table>
  204. </dd></dl>
  205. <dl class="method">
  206. <dt id="task.crontab.cronjob.set_trigger_conditions">
  207. <code class="descname">set_trigger_conditions</code><span class="sig-paren">(</span><em>minute=None</em>, <em>hour=None</em>, <em>day_of_month=None</em>, <em>month=None</em>, <em>day_of_week=None</em><span class="sig-paren">)</span><a class="headerlink" href="#task.crontab.cronjob.set_trigger_conditions" title="Permalink to this definition">¶</a></dt>
  208. <dd><p>This Method changes the execution parameters.</p>
  209. <table class="docutils field-list" frame="void" rules="none">
  210. <col class="field-name" />
  211. <col class="field-body" />
  212. <tbody valign="top">
  213. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  214. <li><strong>minute</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Minute for execution. Either 0…59, [0…59, 0…59, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Minute.</li>
  215. <li><strong>hour</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Hour for execution. Either 0…23, [0…23, 0…23, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Hour.</li>
  216. <li><strong>day_of_month</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Day of Month for execution. Either 0…31, [0…31, 0…31, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Day of Month.</li>
  217. <li><strong>month</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Month for execution. Either 0…12, [0…12, 0…12, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Month.</li>
  218. <li><strong>day_of_week</strong> (<em>int</em><em>, </em><em>list</em><em>, </em><em>str</em>) – Day of Week for execution. Either 0…6, [0…6, 0…6, …] or <a class="reference internal" href="#task.crontab.ANY" title="task.crontab.ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">crontab.ANY</span></code></a> for every Day of Week.</li>
  219. </ul>
  220. </td>
  221. </tr>
  222. </tbody>
  223. </table>
  224. </dd></dl>
  225. </dd></dl>
  226. </dd></dl>
  227. <dl class="class">
  228. <dt id="task.delayed">
  229. <em class="property">class </em><code class="descclassname">task.</code><code class="descname">delayed</code><span class="sig-paren">(</span><em>cycle_time</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#task.delayed" title="Permalink to this definition">¶</a></dt>
  230. <dd><p>Class to execute a callback a given time in the future. See also parent <a class="reference internal" href="#task.periodic" title="task.periodic"><code class="xref py py-class docutils literal notranslate"><span class="pre">periodic</span></code></a>.</p>
  231. <table class="docutils field-list" frame="void" rules="none">
  232. <col class="field-name" />
  233. <col class="field-body" />
  234. <tbody valign="top">
  235. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  236. <li><strong>time</strong> (<em>float</em>) – Delay time for execution of the given callback</li>
  237. <li><strong>callback</strong> (<em>callback</em>) – Callback to be executed</li>
  238. <li><strong>args</strong> (<em>args</em>) – Arguments to be given to callback</li>
  239. <li><strong>kwargs</strong> (<em>kwargs</em>) – Keword Arguments to be given to callback</li>
  240. </ul>
  241. </td>
  242. </tr>
  243. </tbody>
  244. </table>
  245. <p><strong>Example:</strong></p>
  246. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  247. <span class="c1"># -*- coding: UTF-8 -*-</span>
  248. <span class="kn">import</span> <span class="nn">sys</span>
  249. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  250. <span class="kn">import</span> <span class="nn">task</span>
  251. <span class="kn">import</span> <span class="nn">time</span>
  252. <span class="k">def</span> <span class="nf">time_print</span><span class="p">(</span><span class="n">txt</span><span class="p">):</span>
  253. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;: &#39;</span> <span class="o">+</span> <span class="n">txt</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  254. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;task.delayed example:</span><span class="se">\n</span><span class="s2">---------------------&quot;</span><span class="p">)</span>
  255. <span class="n">dt</span> <span class="o">=</span> <span class="n">task</span><span class="o">.</span><span class="n">delayed</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">time_print</span><span class="p">,</span> <span class="s2">&quot;A delayed hello!&quot;</span><span class="p">)</span>
  256. <span class="n">dt</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  257. <span class="k">try</span><span class="p">:</span>
  258. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;starting...&quot;</span><span class="p">)</span>
  259. <span class="n">dt</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  260. <span class="k">finally</span><span class="p">:</span>
  261. <span class="n">dt</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  262. </pre></div>
  263. </div>
  264. <p>Will result to the following output:</p>
  265. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>task.delayed example:
  266. ---------------------
  267. Thu Jan 7 17:18:01 2021: starting...
  268. Thu Jan 7 17:18:06 2021: A delayed hello!
  269. </pre></div>
  270. </div>
  271. <dl class="method">
  272. <dt id="task.delayed.run">
  273. <code class="descname">run</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.delayed.run" title="Permalink to this definition">¶</a></dt>
  274. <dd><p>This starts the timer for the delayed execution.</p>
  275. </dd></dl>
  276. </dd></dl>
  277. <dl class="class">
  278. <dt id="task.periodic">
  279. <em class="property">class </em><code class="descclassname">task.</code><code class="descname">periodic</code><span class="sig-paren">(</span><em>cycle_time</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#task.periodic" title="Permalink to this definition">¶</a></dt>
  280. <dd><p>Class to execute a callback cyclicly.</p>
  281. <table class="docutils field-list" frame="void" rules="none">
  282. <col class="field-name" />
  283. <col class="field-body" />
  284. <tbody valign="top">
  285. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  286. <li><strong>cycle_time</strong> (<em>float</em>) – Cycle time in seconds – callback will be executed every <em>cycle_time</em> seconds</li>
  287. <li><strong>callback</strong> (<em>callback</em>) – Callback to be executed</li>
  288. <li><strong>args</strong> (<em>args</em>) – Arguments to be given to the callback</li>
  289. <li><strong>kwargs</strong> (<em>kwargs</em>) – Keword Arguments to be given to callback</li>
  290. </ul>
  291. </td>
  292. </tr>
  293. </tbody>
  294. </table>
  295. <div class="admonition note">
  296. <p class="first admonition-title">Note</p>
  297. <p class="last">The Callback will get this instance as first argument, followed by <code class="xref py py-data docutils literal notranslate"><span class="pre">args</span></code> und <code class="xref py py-data docutils literal notranslate"><span class="pre">kwargs</span></code>.</p>
  298. </div>
  299. <p><strong>Example:</strong></p>
  300. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  301. <span class="c1"># -*- coding: UTF-8 -*-</span>
  302. <span class="kn">import</span> <span class="nn">sys</span>
  303. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  304. <span class="kn">import</span> <span class="nn">task</span>
  305. <span class="kn">import</span> <span class="nn">time</span>
  306. <span class="n">task_num</span> <span class="o">=</span> <span class="mi">0</span>
  307. <span class="k">def</span> <span class="nf">time_print</span><span class="p">(</span><span class="n">txt</span><span class="p">):</span>
  308. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;: &#39;</span> <span class="o">+</span> <span class="n">txt</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  309. <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="n">rt</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
  310. <span class="k">global</span> <span class="n">task_num</span>
  311. <span class="n">task_num</span> <span class="o">+=</span> <span class="mi">1</span>
  312. <span class="k">if</span> <span class="n">task_num</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">:</span>
  313. <span class="n">rt</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  314. <span class="n">tn</span> <span class="o">=</span> <span class="n">task_num</span>
  315. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;(Task </span><span class="si">%d</span><span class="s2">) Hello </span><span class="si">%s</span><span class="s2">!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tn</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
  316. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span>
  317. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;(Task </span><span class="si">%d</span><span class="s2">) Ende!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tn</span><span class="p">))</span>
  318. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;task.periodic example:</span><span class="se">\n</span><span class="s2">----------------------&quot;</span><span class="p">)</span>
  319. <span class="n">pt</span> <span class="o">=</span> <span class="n">task</span><span class="o">.</span><span class="n">periodic</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from periodic example&quot;</span><span class="p">)</span>
  320. <span class="n">pt</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  321. <span class="k">try</span><span class="p">:</span>
  322. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;starting...&quot;</span><span class="p">)</span>
  323. <span class="n">pt</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  324. <span class="k">finally</span><span class="p">:</span>
  325. <span class="n">pt</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  326. </pre></div>
  327. </div>
  328. <p>Will result to the following output:</p>
  329. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>task.periodic example:
  330. ----------------------
  331. Thu Jan 7 17:21:26 2021: starting...
  332. Thu Jan 7 17:21:26 2021: (Task 1) Hello from periodic example!
  333. Thu Jan 7 17:21:28 2021: (Task 2) Hello from periodic example!
  334. Thu Jan 7 17:21:30 2021: (Task 1) Ende!
  335. Thu Jan 7 17:21:30 2021: (Task 3) Hello from periodic example!
  336. Thu Jan 7 17:21:32 2021: (Task 2) Ende!
  337. Thu Jan 7 17:21:32 2021: (Task 4) Hello from periodic example!
  338. Thu Jan 7 17:21:34 2021: (Task 3) Ende!
  339. Thu Jan 7 17:21:34 2021: (Task 5) Hello from periodic example!
  340. Thu Jan 7 17:21:36 2021: (Task 4) Ende!
  341. Thu Jan 7 17:21:38 2021: (Task 5) Ende!
  342. </pre></div>
  343. </div>
  344. <dl class="method">
  345. <dt id="task.periodic.join">
  346. <code class="descname">join</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.periodic.join" title="Permalink to this definition">¶</a></dt>
  347. <dd><p>This blocks till the cyclic task is terminated.</p>
  348. <div class="admonition note">
  349. <p class="first admonition-title">Note</p>
  350. <p class="last">Using join means that somewhere has to be a condition calling <a class="reference internal" href="#task.periodic.stop" title="task.periodic.stop"><code class="xref py py-func docutils literal notranslate"><span class="pre">stop()</span></code></a> to terminate. Otherwise <code class="xref py py-func docutils literal notranslate"><span class="pre">task.join()</span></code> will never return.</p>
  351. </div>
  352. </dd></dl>
  353. <dl class="method">
  354. <dt id="task.periodic.run">
  355. <code class="descname">run</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.periodic.run" title="Permalink to this definition">¶</a></dt>
  356. <dd><p>This starts the cyclic execution of the given callback.</p>
  357. </dd></dl>
  358. <dl class="method">
  359. <dt id="task.periodic.stop">
  360. <code class="descname">stop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.periodic.stop" title="Permalink to this definition">¶</a></dt>
  361. <dd><p>This stops the execution of any further task.</p>
  362. </dd></dl>
  363. </dd></dl>
  364. <dl class="class">
  365. <dt id="task.queue">
  366. <em class="property">class </em><code class="descclassname">task.</code><code class="descname">queue</code><span class="sig-paren">(</span><em>expire=True</em><span class="sig-paren">)</span><a class="headerlink" href="#task.queue" title="Permalink to this definition">¶</a></dt>
  367. <dd><p>Class to execute queued callbacks.</p>
  368. <table class="docutils field-list" frame="void" rules="none">
  369. <col class="field-name" />
  370. <col class="field-body" />
  371. <tbody valign="top">
  372. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>expire</strong> (<em>bool</em>) – The default value for expire. See also <a class="reference internal" href="#task.queue.expire" title="task.queue.expire"><code class="xref py py-func docutils literal notranslate"><span class="pre">expire()</span></code></a>.</td>
  373. </tr>
  374. </tbody>
  375. </table>
  376. <p><strong>Example:</strong></p>
  377. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  378. <span class="c1"># -*- coding: UTF-8 -*-</span>
  379. <span class="kn">import</span> <span class="nn">sys</span>
  380. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  381. <span class="kn">import</span> <span class="nn">task</span>
  382. <span class="kn">import</span> <span class="nn">time</span>
  383. <span class="n">task_num</span> <span class="o">=</span> <span class="mi">0</span>
  384. <span class="k">def</span> <span class="nf">time_print</span><span class="p">(</span><span class="n">txt</span><span class="p">):</span>
  385. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;: &#39;</span> <span class="o">+</span> <span class="n">txt</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  386. <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="n">rt</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
  387. <span class="k">global</span> <span class="n">task_num</span>
  388. <span class="n">task_num</span> <span class="o">+=</span> <span class="mi">1</span>
  389. <span class="k">if</span> <span class="n">task_num</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">:</span>
  390. <span class="n">rt</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  391. <span class="n">tn</span> <span class="o">=</span> <span class="n">task_num</span>
  392. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;(Task </span><span class="si">%d</span><span class="s2">) Hello </span><span class="si">%s</span><span class="s2">!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tn</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
  393. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span>
  394. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;(Task </span><span class="si">%d</span><span class="s2">) Ende!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tn</span><span class="p">))</span>
  395. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;task.queue example:</span><span class="se">\n</span><span class="s2">----------------------&quot;</span><span class="p">)</span>
  396. <span class="n">q</span> <span class="o">=</span> <span class="n">task</span><span class="o">.</span><span class="n">queue</span><span class="p">()</span>
  397. <span class="n">q</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from queue example (5)&quot;</span><span class="p">)</span>
  398. <span class="n">q</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from queue example (6)&quot;</span><span class="p">)</span>
  399. <span class="n">q</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from queue example (4)&quot;</span><span class="p">)</span>
  400. <span class="n">q</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  401. </pre></div>
  402. </div>
  403. <p>Will result to the following output:</p>
  404. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>task.queue example:
  405. ----------------------
  406. Thu Jan 7 17:30:54 2021: (Task 1) Hello from queue example (4)!
  407. Thu Jan 7 17:30:58 2021: (Task 1) Ende!
  408. Thu Jan 7 17:30:58 2021: (Task 2) Hello from queue example (5)!
  409. Thu Jan 7 17:31:02 2021: (Task 2) Ende!
  410. Thu Jan 7 17:31:02 2021: (Task 3) Hello from queue example (6)!
  411. Thu Jan 7 17:31:06 2021: (Task 3) Ende!
  412. </pre></div>
  413. </div>
  414. <dl class="method">
  415. <dt id="task.queue.clean_queue">
  416. <code class="descname">clean_queue</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.queue.clean_queue" title="Permalink to this definition">¶</a></dt>
  417. <dd><p>This Methods removes all jobs from the queue.</p>
  418. <div class="admonition note">
  419. <p class="first admonition-title">Note</p>
  420. <p class="last">Be aware that already running jobs will not be terminated.</p>
  421. </div>
  422. </dd></dl>
  423. <dl class="method">
  424. <dt id="task.queue.enqueue">
  425. <code class="descname">enqueue</code><span class="sig-paren">(</span><em>priority</em>, <em>callback</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#task.queue.enqueue" title="Permalink to this definition">¶</a></dt>
  426. <dd><p>This enqueues a given callback.</p>
  427. <table class="docutils field-list" frame="void" rules="none">
  428. <col class="field-name" />
  429. <col class="field-body" />
  430. <tbody valign="top">
  431. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  432. <li><strong>priority</strong> (<em>number</em>) – The priority indication number of this task. The lowest value will be queued first.</li>
  433. <li><strong>callback</strong> (<em>callback</em>) – Callback to be executed</li>
  434. <li><strong>args</strong> (<em>args</em>) – Arguments to be given to callback</li>
  435. <li><strong>kwargs</strong> (<em>kwargs</em>) – Keword Arguments to be given to callback</li>
  436. </ul>
  437. </td>
  438. </tr>
  439. </tbody>
  440. </table>
  441. <div class="admonition note">
  442. <p class="first admonition-title">Note</p>
  443. <p class="last">Callback will get this instance as first argument, followed by <code class="xref py py-data docutils literal notranslate"><span class="pre">args</span></code> und <code class="xref py py-data docutils literal notranslate"><span class="pre">kwargs</span></code>.</p>
  444. </div>
  445. </dd></dl>
  446. <dl class="method">
  447. <dt id="task.queue.expire">
  448. <code class="descname">expire</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.queue.expire" title="Permalink to this definition">¶</a></dt>
  449. <dd><p>This sets the expire flag. That means that the process will stop after queue gets empty.</p>
  450. </dd></dl>
  451. <dl class="method">
  452. <dt id="task.queue.run">
  453. <code class="descname">run</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.queue.run" title="Permalink to this definition">¶</a></dt>
  454. <dd><p>This starts the execution of the queued callbacks.</p>
  455. </dd></dl>
  456. <dl class="method">
  457. <dt id="task.queue.stop">
  458. <code class="descname">stop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.queue.stop" title="Permalink to this definition">¶</a></dt>
  459. <dd><p>This sets the stop flag. That means that the process will stop after finishing the active task.</p>
  460. </dd></dl>
  461. </dd></dl>
  462. <dl class="class">
  463. <dt id="task.threaded_queue">
  464. <em class="property">class </em><code class="descclassname">task.</code><code class="descname">threaded_queue</code><span class="sig-paren">(</span><em>expire=False</em><span class="sig-paren">)</span><a class="headerlink" href="#task.threaded_queue" title="Permalink to this definition">¶</a></dt>
  465. <dd><p>Class to execute queued callbacks in a background thread (See also parent <a class="reference internal" href="#task.queue" title="task.queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue</span></code></a>).</p>
  466. <table class="docutils field-list" frame="void" rules="none">
  467. <col class="field-name" />
  468. <col class="field-body" />
  469. <tbody valign="top">
  470. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>expire</strong> (<em>bool</em>) – The default value for expire. See also <a class="reference internal" href="#task.queue.expire" title="task.queue.expire"><code class="xref py py-func docutils literal notranslate"><span class="pre">queue.expire()</span></code></a>.</td>
  471. </tr>
  472. </tbody>
  473. </table>
  474. <p><strong>Example:</strong></p>
  475. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  476. <span class="c1"># -*- coding: UTF-8 -*-</span>
  477. <span class="kn">import</span> <span class="nn">sys</span>
  478. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  479. <span class="kn">import</span> <span class="nn">task</span>
  480. <span class="kn">import</span> <span class="nn">time</span>
  481. <span class="n">task_num</span> <span class="o">=</span> <span class="mi">0</span>
  482. <span class="k">def</span> <span class="nf">time_print</span><span class="p">(</span><span class="n">txt</span><span class="p">):</span>
  483. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;: &#39;</span> <span class="o">+</span> <span class="n">txt</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  484. <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="n">rt</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
  485. <span class="k">global</span> <span class="n">task_num</span>
  486. <span class="n">task_num</span> <span class="o">+=</span> <span class="mi">1</span>
  487. <span class="k">if</span> <span class="n">task_num</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">:</span>
  488. <span class="n">rt</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  489. <span class="n">tn</span> <span class="o">=</span> <span class="n">task_num</span>
  490. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;(Task </span><span class="si">%d</span><span class="s2">) Hello </span><span class="si">%s</span><span class="s2">!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tn</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
  491. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span>
  492. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;(Task </span><span class="si">%d</span><span class="s2">) Ende!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tn</span><span class="p">))</span>
  493. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;task.threaded_queue example:</span><span class="se">\n</span><span class="s2">-------------------------------&quot;</span><span class="p">)</span>
  494. <span class="n">tq</span> <span class="o">=</span> <span class="n">task</span><span class="o">.</span><span class="n">threaded_queue</span><span class="p">()</span>
  495. <span class="n">tq</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from queue example (5)&quot;</span><span class="p">)</span>
  496. <span class="n">tq</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from queue example (6)&quot;</span><span class="p">)</span>
  497. <span class="n">tq</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">hello</span><span class="p">,</span> <span class="s2">&quot;from queue example (4)&quot;</span><span class="p">)</span>
  498. <span class="n">tq</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  499. <span class="k">try</span><span class="p">:</span>
  500. <span class="n">time_print</span><span class="p">(</span><span class="s2">&quot;starting...&quot;</span><span class="p">)</span>
  501. <span class="n">tq</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  502. <span class="k">finally</span><span class="p">:</span>
  503. <span class="n">tq</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  504. </pre></div>
  505. </div>
  506. <p>Will result to the following output:</p>
  507. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>task.threaded_queue example:
  508. -------------------------------
  509. Thu Jan 7 17:33:50 2021: (Task 1) Hello from queue example (4)!
  510. Thu Jan 7 17:33:50 2021: starting...
  511. Thu Jan 7 17:33:54 2021: (Task 1) Ende!
  512. Thu Jan 7 17:33:54 2021: (Task 2) Hello from queue example (5)!
  513. Thu Jan 7 17:33:58 2021: (Task 2) Ende!
  514. Thu Jan 7 17:33:58 2021: (Task 3) Hello from queue example (6)!
  515. Thu Jan 7 17:34:01 2021: (Task 3) Ende!
  516. </pre></div>
  517. </div>
  518. <dl class="method">
  519. <dt id="task.threaded_queue.join">
  520. <code class="descname">join</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.threaded_queue.join" title="Permalink to this definition">¶</a></dt>
  521. <dd><p>This blocks till the queue is empty.</p>
  522. <div class="admonition note">
  523. <p class="first admonition-title">Note</p>
  524. <p class="last">If the queue does not run dry, join will block till the end of the days.</p>
  525. </div>
  526. </dd></dl>
  527. <dl class="method">
  528. <dt id="task.threaded_queue.run">
  529. <code class="descname">run</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.threaded_queue.run" title="Permalink to this definition">¶</a></dt>
  530. <dd><p>This starts the execution of the queued callbacks.</p>
  531. </dd></dl>
  532. <dl class="method">
  533. <dt id="task.threaded_queue.stop">
  534. <code class="descname">stop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#task.threaded_queue.stop" title="Permalink to this definition">¶</a></dt>
  535. <dd><p>This sets the stop flag. That means that the process will stop after finishing the active task.</p>
  536. </dd></dl>
  537. </dd></dl>
  538. </div>
  539. <div class="toctree-wrapper compound">
  540. </div>
  541. </div>
  542. <div class="section" id="indices-and-tables">
  543. <h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h1>
  544. <ul class="simple">
  545. <li><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></li>
  546. <li><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></li>
  547. <li><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></li>
  548. </ul>
  549. </div>
  550. </div>
  551. </div>
  552. <footer>
  553. <hr/>
  554. <div role="contentinfo">
  555. <p>
  556. &copy; Copyright 2021, Dirk Alders
  557. </p>
  558. </div>
  559. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
  560. <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
  561. provided by <a href="https://readthedocs.org">Read the Docs</a>.
  562. </footer>
  563. </div>
  564. </div>
  565. </section>
  566. </div>
  567. <script type="text/javascript">
  568. jQuery(function () {
  569. SphinxRtdTheme.Navigation.enable(true);
  570. });
  571. </script>
  572. </body>
  573. </html>