File: //usr/share/doc/python3-django-q/html/signals.html
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Signals — Django Q2 1.6.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b76e3c8a" />
<link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=86f27845" />
<link rel="shortcut icon" href="_static/favicon.ico"/>
<script src="_static/jquery.js?v=8dae8fb0"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="_static/documentation_options.js?v=d6a008b6"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/js/theme.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Architecture" href="architecture.html" />
<link rel="prev" title="Errors" href="errors.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="index.html" class="icon icon-home">
Django Q2
</a>
<div class="version">
1.6
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="install.html"> Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="configure.html"> Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="brokers.html"> Brokers</a></li>
<li class="toctree-l1"><a class="reference internal" href="tasks.html"> Tasks</a></li>
<li class="toctree-l1"><a class="reference internal" href="group.html"> Groups</a></li>
<li class="toctree-l1"><a class="reference internal" href="iterable.html"> Iterable</a></li>
<li class="toctree-l1"><a class="reference internal" href="chain.html"> Chains</a></li>
<li class="toctree-l1"><a class="reference internal" href="schedules.html"> Schedules</a></li>
<li class="toctree-l1"><a class="reference internal" href="cluster.html"> Cluster</a></li>
<li class="toctree-l1"><a class="reference internal" href="monitor.html"> Monitor</a></li>
<li class="toctree-l1"><a class="reference internal" href="admin.html"> Admin</a></li>
<li class="toctree-l1"><a class="reference internal" href="errors.html"> Errors</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#"> Signals</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#available-signals">Available signals</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#before-enqueuing-a-task">Before enqueuing a task</a></li>
<li class="toctree-l3"><a class="reference internal" href="#after-spawning-a-worker-process">After spawning a worker process</a></li>
<li class="toctree-l3"><a class="reference internal" href="#before-executing-a-task">Before executing a task</a></li>
<li class="toctree-l3"><a class="reference internal" href="#after-executing-a-task">After executing a task</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#subscribing-to-a-signal">Subscribing to a signal</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="architecture.html"> Architecture</a></li>
<li class="toctree-l1"><a class="reference internal" href="examples.html"> Examples</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Django Q2</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item active">Signals</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/signals.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="signals">
<h1>Signals<a class="headerlink" href="#signals" title="Link to this heading"></a></h1>
<section id="available-signals">
<h2>Available signals<a class="headerlink" href="#available-signals" title="Link to this heading"></a></h2>
<p>Django Q2 emits the following signals during its lifecycle.</p>
<section id="before-enqueuing-a-task">
<h3>Before enqueuing a task<a class="headerlink" href="#before-enqueuing-a-task" title="Link to this heading"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">django_q.signals.pre_enqueue</span></code> signal is emitted before a task is
enqueued. The task dictionary is given as the <code class="docutils literal notranslate"><span class="pre">task</span></code> argument.</p>
</section>
<section id="after-spawning-a-worker-process">
<h3>After spawning a worker process<a class="headerlink" href="#after-spawning-a-worker-process" title="Link to this heading"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">django_q.signals.post_spawn</span></code> signal is emitted after a worker process has
spawned. The process name is given as the <code class="docutils literal notranslate"><span class="pre">proc_name</span></code> argument (string).</p>
</section>
<section id="before-executing-a-task">
<h3>Before executing a task<a class="headerlink" href="#before-executing-a-task" title="Link to this heading"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">django_q.signals.pre_execute</span></code> signal is emitted before a task is
executed by a worker. This signal provides two arguments:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">task</span></code>: the task dictionary.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">func</span></code>: the actual function that will be executed. If the task was created
with a function path, this argument will be the callable function
nonetheless.</p></li>
</ul>
</section>
<section id="after-executing-a-task">
<h3>After executing a task<a class="headerlink" href="#after-executing-a-task" title="Link to this heading"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">django_q.signals.post_execute</span></code> signal is emitted after a task is
executed by a worker and processed by the monitor. It included the <code class="docutils literal notranslate"><span class="pre">task</span></code> dictionary with the result.</p>
</section>
</section>
<section id="subscribing-to-a-signal">
<h2>Subscribing to a signal<a class="headerlink" href="#subscribing-to-a-signal" title="Link to this heading"></a></h2>
<p>Connecting to a Django Q2 signal is done the same as any other Django
signal:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.dispatch</span> <span class="kn">import</span> <span class="n">receiver</span>
<span class="kn">from</span> <span class="nn">django_q.signals</span> <span class="kn">import</span> <span class="n">pre_enqueue</span><span class="p">,</span> <span class="n">pre_execute</span><span class="p">,</span> <span class="n">post_execute</span><span class="p">,</span> <span class="n">post_spawn</span>
<span class="nd">@receiver</span><span class="p">(</span><span class="n">pre_enqueue</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_pre_enqueue_callback</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">task</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Task </span><span class="si">{</span><span class="n">task</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span><span class="si">}</span><span class="s2"> will be queued"</span><span class="p">)</span>
<span class="nd">@receiver</span><span class="p">(</span><span class="n">pre_execute</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_pre_execute_callback</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">task</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Task </span><span class="si">{</span><span class="n">task</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span><span class="si">}</span><span class="s2"> will be executed by calling </span><span class="si">{</span><span class="n">func</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nd">@receiver</span><span class="p">(</span><span class="n">post_execute</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_post_execute_callback</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">task</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Task </span><span class="si">{</span><span class="n">task</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span><span class="si">}</span><span class="s2"> was executed with result </span><span class="si">{</span><span class="n">task</span><span class="p">[</span><span class="s1">'result'</span><span class="p">]</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nd">@receiver</span><span class="p">(</span><span class="n">post_spawn</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">my_post_spawn_callback</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">proc_name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Process </span><span class="si">{</span><span class="n">proc_name</span><span class="si">}</span><span class="s2"> has spawned"</span><span class="p">)</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="errors.html" class="btn btn-neutral float-left" title="Errors" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="architecture.html" class="btn btn-neutral float-right" title="Architecture" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2015-2023, Ilan Steemers - 2022, Stan Triepels.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>