<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by VATSAL PATEL on Medium]]></title>
        <description><![CDATA[Stories by VATSAL PATEL on Medium]]></description>
        <link>https://medium.com/@vatsal5345?source=rss-15416c65bcf5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*NSfShcOJ4Rmw3y-0</url>
            <title>Stories by VATSAL PATEL on Medium</title>
            <link>https://medium.com/@vatsal5345?source=rss-15416c65bcf5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:10:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vatsal5345/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Do Background Job Using Django Celery]]></title>
            <link>https://awstip.com/do-background-job-using-django-celery-5aae1b3e8a3a?source=rss-15416c65bcf5------2</link>
            <guid isPermaLink="false">https://medium.com/p/5aae1b3e8a3a</guid>
            <category><![CDATA[celery-workers]]></category>
            <category><![CDATA[celery-django]]></category>
            <category><![CDATA[django-background-tasks]]></category>
            <category><![CDATA[send-email-using-celery]]></category>
            <category><![CDATA[send-email-in-background]]></category>
            <dc:creator><![CDATA[VATSAL PATEL]]></dc:creator>
            <pubDate>Mon, 05 Dec 2022 11:15:29 GMT</pubDate>
            <atom:updated>2022-12-19T17:40:01.188Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sending Email Using Django Celery</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*viyC6TuFLc9JiR6ZlarXWw.jpeg" /></figure><h3><strong><em>What is Celery ??</em></strong></h3><p>Celery is a distributed task queue that can collect, record, schedule, and perform tasks outside of your main program.</p><p>To receive a task from the program and run it in the background, Celery requires a message broker for communication.</p><p><a href="https://redis.io/"><strong>Redis</strong></a> and <a href="https://www.rabbitmq.com/"><strong>RabbitMQ</strong></a> are two message brokers that developers often use together with Celery.</p><p>In this tutorial, you’ll use <a href="https://redis.io/"><strong>Redis</strong></a> as the message broker.</p><blockquote><strong>workers</strong> is the background-based task servers. In an application with web servers, we can have several workers perform heavy computations in the background and send back the response to the application through webhooks or callbacks.</blockquote><blockquote><strong>Celery beat</strong> is a scheduler that arranges when to run tasks. You can use it to schedule periodic tasks as well.</blockquote><p>Celery workers are the backbone of Celery. Even if you aim to schedule recurring tasks using Celery beat, a Celery worker will pick up your instructions and handle them at the scheduled time.</p><h3><strong>Why Use Celery ??</strong></h3><p>Mainly two reasons that most Developers want to start using Celery :</p><ol><li>Reduce load from the app and do that task in the background independently.</li><li>Scheduling tasks at a specific time.</li></ol><p>Celery can help run tasks on the worker process instead of the web process, so in the web process we can return the HTTP response back immediately (even if the task in the worker process is still running) to our user, and the request cycle would not be blocked and the user experience would be better.</p><h3>Install Celery and Redis in Django</h3><pre>pip install celery<br><br>pip install redis</pre><h3>Integrate Celery With Django</h3><p>Here we create a project named “<strong>ProjectCelery</strong>” and create an app named “<strong>myapp</strong>”.</p><p>Folder Structure:</p><pre><strong>ProjectCelery/</strong><br>├── manage.py<br>├── myapp<br>│   ├── admin.py<br>│   ├── apps.py<br>│   ├── __init__.py<br>│   ├── migrations<br>│   │   └── __init__.py<br>│   ├── models.py<br>│   ├── tests.py<br>│   └── views.py<br>└── ProjectCelery<br>    ├── asgi.py<br>    ├── __init__.py<br>    ├── settings.py<br>    ├── urls.py   <br>    └── wsgi.py</pre><ol><li>Now, create a <strong>celery.py</strong> file that defines the celery instance, inside the <strong>ProjectCelery</strong> folder.</li></ol><p><strong>file: ProjectCelery/ProjectCelery/celery.py</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/563054f961928879a3a8a2f6a3255b00/href">https://medium.com/media/563054f961928879a3a8a2f6a3255b00/href</a></iframe><p>2. Then you need to import this app into your <strong>__init__.py </strong>file of the <strong>ProjectCelery</strong> folder. This ensures that the app is loaded when Django starts so that the <strong>@shared_task</strong> decorator (mentioned later) will use it:</p><p><strong>file: ProjectCelery/ProjectCelery/__init__.py</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c62424f29b8b6e3af53b278351d36b95/href">https://medium.com/media/c62424f29b8b6e3af53b278351d36b95/href</a></iframe><p>3. Now we add the configuration for celery in <strong>settings.py </strong>of the <strong>ProjectCelery </strong>folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63ce319e1d217d718f38e3b5fac18094/href">https://medium.com/media/63ce319e1d217d718f38e3b5fac18094/href</a></iframe><p>Our configuration for celery is completed here.</p><p>4. Now we are doing the configuration for sending emails.</p><p>Here we take an example of sending mail in the background using Celery.</p><p>we have to make a configuration for sending emails from GMAIL in <strong>settings.py </strong>of the <strong>ProjectCelery </strong>folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1706c44f937140f199a3bbff0b5a5d78/href">https://medium.com/media/1706c44f937140f199a3bbff0b5a5d78/href</a></iframe><p>To get your <strong>Gmail app password</strong>: <a href="https://support.google.com/accounts/answer/185833"><em>Click Here</em></a></p><p>5. Now add a <strong>task.py</strong> file in our app in which we add the task that we want to do using celery. Here we make a function for sending an email using celery.</p><p>To transform this function into a Celery task, all you need to do is decorate it with <strong>@shared_task</strong>, which you import from the celery.</p><p><strong>file path: ProjectCelery/myapp/task.py</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80492c33c7d3fbbc40711281d74e5ea4/href">https://medium.com/media/80492c33c7d3fbbc40711281d74e5ea4/href</a></iframe><p>6. Now, make a function <strong>send_email</strong> for sending mail in the <strong>views.py </strong>file of <strong>my app </strong>folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03c887a680d1116637cf1723080efa62/href">https://medium.com/media/03c887a680d1116637cf1723080efa62/href</a></iframe><p>Here we use <strong>.delay()</strong> to call the specific task which we want to run in the background.</p><p>7. Now start the l<strong>ocal server</strong> and <strong>Celery worker </strong>in <strong>different Terminal </strong>:</p><pre>python manage.py runserver  <strong>#to start local server</strong><br><br>celery -A ProjectCelery worker --loglevel=info  <strong>#to start celery worker</strong>  </pre><p>when the function “<strong>send_email</strong>” is called, the response of “<strong>Sent Email Successfully…Check your mail please</strong>” comes immediately but the email is not sent yet because we make a sleep time of 20 seconds but this process is running in the background in celery worker. and after 20 seconds the email is sent to the receiver.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5aae1b3e8a3a" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/do-background-job-using-django-celery-5aae1b3e8a3a">Do Background Job Using Django Celery</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>