<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ celery - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ celery - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 20 Sep 2026 11:38:08 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/celery/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use Celery in Django ]]>
                </title>
                <description>
                    <![CDATA[ You’ve probably noticed that some tasks in your Django app seem to take a long time. For example, maybe sending confirmation emails, resizing images, or processing large data files slows things down. The good news? You don’t have to sit around waitin... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-celery-in-django/</link>
                <guid isPermaLink="false">68027fc44d4422c6f797b252</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ celery ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Django ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Udemezue John ]]>
                </dc:creator>
                <pubDate>Fri, 18 Apr 2025 16:37:24 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744994231247/63228755-1929-4474-9930-15f8ff1a5631.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>You’ve probably noticed that some tasks in your Django app seem to take a long time. For example, maybe sending confirmation emails, resizing images, or processing large data files slows things down.</p>
<p>The good news? You don’t have to sit around waiting. You can hand those tasks off to something else and let your app keep doing its thing. That "something else" is Celery.</p>
<p>Celery lets you run time-consuming tasks in the background while your app stays fast. And if you're using Django, it's actually not that hard to plug it in – once you understand how the pieces work together.</p>
<p>In this guide, I’ll walk you through what Celery is, why it’s useful, and exactly how to set it up with Django step by step.</p>
<h2 id="heading-table-of-contents">Table Of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-what-is-celery-and-why-should-you-use-it-in-django">What Is Celery and Why Should You Use It in Django?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-celery-works-the-simple-version">How Celery Works (The Simple Version)</a></p>
</li>
<li><p><a class="post-section-overview" href="#how-to-use-celery-in-django">How to Use Celery in Django</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-1-install-the-right-packages">1. Install the right packages</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-2-create-a-celerypy-file-in-your-project-folder">2. Create a</a> <a target="_blank" href="http://celery.py">celery.py</a> <a class="post-section-overview" href="#heading-2-create-a-celerypy-file-in-your-project-folder">file in your project folder</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-3-add-celery-to-initpy">3. Add Celery to</a> <a target="_blank" href="http://init.py"><strong>init</strong>.py</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-4-set-the-broker-url-in-your-settings">4. Set the broker URL in your settings</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-5-write-your-first-task">5. Write your first task</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-6-call-the-task-from-your-views">6. Call the task from your views</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-7-run-the-celery-worker">7. Run the Celery worker</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-optional-using-django-admin-to-monitor-tasks">Optional: Using Django Admin to Monitor Tasks</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-faq">FAQ</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-happens-if-redis-goes-down">What happens if Redis goes down?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-can-i-retry-failed-tasks">Can I retry failed tasks?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-is-celery-the-only-option">Is Celery the only option?</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-wrapping-it-up">Wrapping It Up</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-further-reading-and-resources">Further Reading and Resources</a></p>
</li>
</ol>
<h2 id="heading-what-is-celery-and-why-should-you-use-it-in-django">What Is Celery and Why Should You Use It in Django?</h2>
<p>Imagine you’re running an online shop. Someone places an order. You want to:</p>
<ul>
<li><p>Save the order to the database</p>
</li>
<li><p>Send them an invoice by email</p>
</li>
<li><p>Notify your warehouse</p>
</li>
<li><p>Maybe even start printing a shipping label</p>
</li>
</ul>
<p>If your app tries to do all this at once, your user is going to be stuck staring at a loading screen. Instead, what if you only saved the order right away – and passed the rest to Celery to handle in the background?</p>
<p>That’s exactly what Celery does.</p>
<p>It’s a task queue — which just means it runs things later, so your main app doesn’t have to wait. It’s especially helpful for:</p>
<ul>
<li><p>Sending emails</p>
</li>
<li><p>Data imports/exports</p>
</li>
<li><p>Running machine learning models</p>
</li>
<li><p>Scraping data</p>
</li>
<li><p>Generating reports</p>
</li>
</ul>
<p>And yeah, it works really well with Django.</p>
<h2 id="heading-how-celery-works-the-simple-version">How Celery Works (The Simple Version)</h2>
<p>Celery is made up of a few parts:</p>
<ol>
<li><p><strong>Task producer (your Django app)</strong> – This is where you call a task.</p>
</li>
<li><p><strong>Broker (usually Redis)</strong> – This is the middleman. It takes the task and holds it until a worker can grab it.</p>
</li>
<li><p><strong>Worker</strong> – This is Celery’s background process that grabs tasks from the broker and runs them.</p>
</li>
</ol>
<p>Here’s the flow:</p>
<pre><code class="lang-plaintext">Django app → Redis → Celery Worker → Done ✅
</code></pre>
<p>Now let’s actually set this up.</p>
<h2 id="heading-how-to-use-celery-in-django">How to Use Celery in Django</h2>
<h3 id="heading-1-install-the-right-packages">1. Install the right packages</h3>
<p>You’ll need <code>celery</code> and a message broker. Redis is a popular choice.</p>
<pre><code class="lang-bash">pip install celery redis
</code></pre>
<p>Also make sure you have Redis running. You can install it locally via Homebrew (<code>brew install redis</code>) or use a Docker container.</p>
<p>If you’re using Docker:</p>
<pre><code class="lang-bash">docker run -p 6379:6379 redis
</code></pre>
<h3 id="heading-2-create-a-celerypy-file-in-your-project-folder">2. Create a <code>celery.py</code> file in your project folder</h3>
<p>Let’s say your Django project is called <code>myproject</code>. Inside that same folder (where <code>settings.py</code> is), create a file called <code>celery.py</code>.</p>
<pre><code class="lang-python"><span class="hljs-comment"># myproject/celery.py</span>
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">from</span> celery <span class="hljs-keyword">import</span> Celery

os.environ.setdefault(<span class="hljs-string">"DJANGO_SETTINGS_MODULE"</span>, <span class="hljs-string">"myproject.settings"</span>)

app = Celery(<span class="hljs-string">"myproject"</span>)

app.config_from_object(<span class="hljs-string">"django.conf:settings"</span>, namespace=<span class="hljs-string">"CELERY"</span>)
app.autodiscover_tasks()
</code></pre>
<p>Here’s what’s happening:</p>
<ul>
<li><p><code>os.environ...</code> sets up Django’s settings.</p>
</li>
<li><p><code>Celery("myproject")</code> creates a new Celery app with your project name.</p>
</li>
<li><p><code>app.config_from_object(...)</code> tells Celery to read config from Django’s settings file.</p>
</li>
<li><p><code>autodiscover_tasks()</code> tells Celery to find tasks in your Django apps automatically.</p>
</li>
</ul>
<h3 id="heading-3-add-celery-to-initpy">3. Add Celery to <code>__init__.py</code></h3>
<p>Still in your <code>myproject/</code> folder, open <code>__init__.py</code> and add:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> .celery <span class="hljs-keyword">import</span> app <span class="hljs-keyword">as</span> celery_app

__all__ = (<span class="hljs-string">"celery_app"</span>,)
</code></pre>
<p>This makes sure Celery starts with Django.</p>
<h3 id="heading-4-set-the-broker-url-in-your-settings">4. Set the broker URL in your settings</h3>
<p>Open <code>settings.py</code> and add:</p>
<pre><code class="lang-python">CELERY_BROKER_URL = <span class="hljs-string">'redis://localhost:6379/0'</span>
</code></pre>
<p>This tells Celery to use Redis as the broker.</p>
<h3 id="heading-5-write-your-first-task">5. Write your first task</h3>
<p>Go to one of your Django apps (say you’ve got an app called <code>shop</code>), and create a file called <code>tasks.py</code>.</p>
<pre><code class="lang-python"><span class="hljs-comment"># shop/tasks.py</span>
<span class="hljs-keyword">from</span> celery <span class="hljs-keyword">import</span> shared_task

<span class="hljs-meta">@shared_task</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_invoice_email</span>(<span class="hljs-params">order_id</span>):</span>
    <span class="hljs-comment"># Imagine this sends an email</span>
    print(<span class="hljs-string">f"Sending invoice email for order <span class="hljs-subst">{order_id}</span>"</span>)
</code></pre>
<p>The <code>@shared_task</code> decorator tells Celery this is a background task.</p>
<h3 id="heading-6-call-the-task-from-your-views">6. Call the task from your views</h3>
<p>Here’s how you’d use it in a Django view:</p>
<pre><code class="lang-python"><span class="hljs-comment"># shop/views.py</span>

<span class="hljs-keyword">from</span> .tasks <span class="hljs-keyword">import</span> send_invoice_email
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">place_order</span>(<span class="hljs-params">request</span>):</span>
    <span class="hljs-comment"># pretend this saves an order</span>
    order_id = <span class="hljs-number">1234</span>  <span class="hljs-comment"># this would come from your model</span>

    <span class="hljs-comment"># run the task in the background</span>
    send_invoice_email.delay(order_id)

    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">"order_complete.html"</span>)
</code></pre>
<p>Notice the <code>.delay()</code> – this is what sends the task to Celery.</p>
<h3 id="heading-7-run-the-celery-worker">7. Run the Celery worker</h3>
<p>Now open a terminal and start the worker:</p>
<pre><code class="lang-bash">celery -A myproject worker --loglevel=info
</code></pre>
<p>You should see the worker start and wait for tasks. When you place an order, it’ll print something like:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">Sending</span> <span class="hljs-selector-tag">invoice</span> <span class="hljs-selector-tag">email</span> <span class="hljs-selector-tag">for</span> <span class="hljs-selector-tag">order</span> 1234
</code></pre>
<h2 id="heading-optional-using-django-admin-to-monitor-tasks">Optional: Using Django Admin to Monitor Tasks</h2>
<p>If you want to monitor task status in the admin, you can use <a target="_blank" href="https://github.com/celery/django-celery-results">django-celery-results</a>.</p>
<pre><code class="lang-bash">pip install django-celery-results
</code></pre>
<p>Then update your <code>settings.py</code>:</p>
<pre><code class="lang-python">INSTALLED_APPS += [<span class="hljs-string">"django_celery_results"</span>]

CELERY_RESULT_BACKEND = <span class="hljs-string">"django-db"</span>
</code></pre>
<p>Run migrations:</p>
<pre><code class="lang-bash">python manage.py migrate
</code></pre>
<p>Now Celery will save task results in your database, and you can see them in Django admin.</p>
<h2 id="heading-faq">FAQ</h2>
<h3 id="heading-what-happens-if-redis-goes-down"><strong>What happens if Redis goes down?</strong></h3>
<p>Your tasks won’t be sent or picked up. But once Redis comes back, things should resume.</p>
<h3 id="heading-can-i-retry-failed-tasks"><strong>Can I retry failed tasks?</strong></h3>
<p>Yes! Celery supports retries. You can set how many times a task should retry and how often. Example:</p>
<pre><code class="lang-python"><span class="hljs-meta">@shared_task(bind=True, max_retries=3)</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">risky_task</span>(<span class="hljs-params">self</span>):</span>
    <span class="hljs-keyword">try</span>:
        <span class="hljs-comment"># Do something risky</span>
        <span class="hljs-keyword">pass</span>
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
        <span class="hljs-keyword">raise</span> self.retry(exc=e, countdown=<span class="hljs-number">60</span>)
</code></pre>
<h3 id="heading-is-celery-the-only-option"><strong>Is Celery the only option?</strong></h3>
<p>No. There’s also Django Q, Dramatiq, and Huey. But Celery is the most mature and has the biggest community.</p>
<h2 id="heading-wrapping-it-up">Wrapping It Up</h2>
<p>Using Celery in Django doesn’t just speed things up – it also helps improve the experience for your users.</p>
<p>Offloading heavy or slow tasks makes your app feel snappier and more reliable.</p>
<p>Once you get the basics down, you’ll find yourself using it for all kinds of things.</p>
<h3 id="heading-further-reading-and-resources">Further Reading and Resources</h3>
<ul>
<li><p><a target="_blank" href="https://docs.celeryq.dev/en/stable/">Celery Documentation</a></p>
</li>
<li><p><a target="_blank" href="https://redis.io/docs/latest/develop/get-started/">Redis Quickstart</a></p>
</li>
<li><p><a target="_blank" href="https://github.com/celery/django-celery-results">django-celery-results</a></p>
</li>
<li><p><a target="_blank" href="https://realpython.com/asynchronous-tasks-with-django-and-celery/">Asynchronous Tasks in Django (Real Python)</a></p>
</li>
<li><p><a target="_blank" href="https://flower.readthedocs.io/en/latest/">Celery Monitoring with Flower</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
