Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question

What is celery?

Celery is a task queue implementation for Python web applications used to asynchronously execute work outside the HTTP request-response cycle.

What do I need?

  • Python (3.6, 3.7, 3.8)
  • Celery version 5.0. 5 runs on, Python (3.6, 3.7, 3.8)
  • redis

Note: Celery uses “brokers” to pass messages between a Django Project and the Celery workers. In this tutorial, we will use Redis as the message broker.

Install Redis as a Celery “Broker”

  • Clone the redis package to your local system by using give link

https://raw.githubusercontent.com/servicestack/redis-windows/master/downloads/redis-latest.zip

  • Open any terminal then goto redis path where you cloned. then type redis-server.exe

This is the project layout:

  • Goto project folder(celerytut) then create file called celery.py (projname/projname/celery.py). add below code into celery.py file

Note: where ever celerytut is there chnage to your project name.

import os

from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celerytut.settings')

app = Celery('celerytut')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.conf.beat_schedule = {
    'add-every-2-seconds':{
        'task' : 'add_2_mumbers',
        'schedule' : 3.0,
    }
}

@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

Import your Celery app

To ensure that the Celery app is loaded when Django starts, add the following code into the init.py file that sits next to your settings.py file and add below code into init.py file.

from .celery import app as celery_app

__all__ = ('celery_app',)

Create tasks.py file under project app. add below code into tasks.py file.

tasks.py
from celery import shared_task
@shared_task(name = "add_2_mumbers")
def myTask():
    print("this is the code should run every 3 seconds")

you can run this task based on time interval.

call the function from views.

views.py
from django.shortcuts import render

# Create your views here.
from .tasks import myTask
myTask.delay()

In settings add below code:

# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Africa/Nairobi'

Running Locally

With your Django App and Redis running, open two new terminal windows/tabs. In each new window, navigate to your project directory, activate your virtualenv, and then run the following commands (one in each window):

<strong>$ celery -A celerytut worker -l info
$ celery -A celerytut beat -l info</strong>

worker should run like this

Beat should run linke this

Final project structure should be like this:

Share
  • Facebook

Sidebar

Ask A Question

Adv 120x600

Looking for advertising?

Consider These Things If You Appreciate What stackorigin Does: Stackorigin is a community where you can ask questions and find answers, for free to everyone. We would appreciate it if you bought us a coffee if you liked what we were doing.

Buy me a Coffe

Adv 120x600

  • Random
  • Answers
    • On: May 7, 2022
    • Answer: 1

    How do oil absorbent rool work

    • On: August 13, 2021
    • Answers: 0

    How to buy books in bulk online for sales purposes

    • On: March 25, 2022
    • Answer: 1

    How to unsubscribe from a YouTube channel on a computer ...

    • On: July 28, 2022
    • Answer: 1

    How to center an iframe horizontally?

    • On: August 9, 2022
    • Answers: 0

    Zovirax compre online, Can zovirax inside mouth

  • strapcart_online
    strapcart_online added an answer Sildenafil citrate is a common ingredient in Cenforce 150mg tablets. This drug… March 21, 2023 at 11:42 am
  • strapcart_online
    strapcart_online added an answer Vilitra 20mg tablets are used to overcome the problem of… March 18, 2023 at 6:13 am
  • ashok
    ashok added an answer Not every girl, there are some girls out there who… March 16, 2023 at 4:15 am
  • strapcart_online
    strapcart_online added an answer Tadarise 40 Tablet is used to permanently remove the problem… March 4, 2023 at 5:48 am
  • strapcart_online
    strapcart_online added an answer Men use Cenforce D tablets for the treatment of sexual… March 1, 2023 at 5:59 am

Adv 120x600

Trending Categories

Health
2042Questions
, 4Followers
Programming
1236Questions
, 0Followers
Technology
216Questions
, 3Followers
General Knowledge
131Questions
, 0Followers
Business & Finance
84Questions
, 4Followers

Adv 120×600

Stats

  • Questions 4k
  • Answers 1k
  • Best Answers 105
  • Users 100

Users

niyadas823

niyadas823

  • 0 Questions
  • 0 Answers
nirala3

nirala3

  • 2 Questions
  • 0 Answers
Perrywalton

Perrywalton

  • 1 Question
  • 0 Answers
veeraa

veeraa

  • 0 Questions
  • 0 Answers
Stewesmitz

Stewesmitz

  • 0 Questions
  • 0 Answers

Adv 120×600

Explore

  • Recent Questions
  • Most Answered
  • Answers
  • No Answers
  • Most Visited
  • Most Voted
  • Random
  • Polls

Footer

Stackorigin - The Community of Question and Answers

Stackorigin

Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

About

  • About Us
  • Contact Us
  • FAQ
  • Submit Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More

Info

  • Privacy Policy
  • Terms and Conditions
  • Community Guidelines
  • Tags

Products

  • Tutorials
  • Advertising
  • Categories
  • Corona
  • StackHow

Follow

© 2022 Stackorigin. All Rights Reserved.