Periodic Activities For Celery: From Creation Till Execution

Posted By : Nitin Sharma | 29-Aug-2022

Django python

Loading...

This is a little illustration of how I used Celery in place of Django to get periodic tasks to function. There are several task creation examples and shaky starting guides for Celery and Celery Beat available, but the most of them use Django. I only wanted to run a very basic example, but I spent much too much time attempting to fill in the blanks so that even this easy activity would run on a regular basis. As a result, I'm hoping that my little example can help someone else out there.

Required:

Python (The main soul of Programs)

Pip (A python module installer)

Setting up Celery

sudo pip install Celery -->(A command for installing Celery module in python)

Make a celery.py File

After installing your preferred broker, you may configure the settings in celery.py. I used MySQL in my situation. Here is an illustration of how the file will appear.

# Change this to your settings in celery.py

BROKER_URL = 'sqla+mysql://user:user_pass@localhost/dbname'

CELERY_RESULT_BACKEND = "database"

CELERY_RESULT_DBURI = 'mysql://user_pass@localhost/dbname'

Write Any Tasks

Set up your tasks (generally they will be saved into a file called tasks.py). What my tasks.py looks like is as follows:

from celery import Celery

celery = Celery('tasks')

celery.config_from_object('celery')

@celery.task

def addition(first_arg, second_arg):

return first_arg + second_arg

Modifying The celery.py File

Then, Modify the celery.py to include the following information so Celery will know how frequently you want to perform your periodic tasks.

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {

'every-minute': {

'task': 'tasks.addition',

'schedule': crontab(), # runs every minute

'args': (8,10),

},

}

Initiating Celery By command in Linux

Our task will now execute every minute if we only start a Celery worker with the —beat flag. The —beat flag must appear after the worker in order for anything to happen; this element was not immediately apparent.

$ celery -A tasks worker --loglevel=info --beat

Final output for which we are curious will look like this :

As jobs are completed, Celery should output a lot of information. You should spot a line that reads something like this among them:

[2022-08-28 13:26:00,343: INFO/MainProcess] Task tasks.addition[444675955

Periodic Activities for Celery(A background tool for python ): From Creation till Execution

This is a little illustration of how I used Celery in place of Django to get Periodic Tasks to function. There are several task creation examples and shaky starting guides for Celery and Celery Beat available, but the most of them use Django. I only wanted to run a very basic example, but I spent much too much time attempting to fill in the blanks so that even this easy activity would run on a regular basis. As a result, I'm hoping that my little example can help someone else out there.

Required:

Python (The main soul of Programs)

Pip (A python module installer)

Setting Up Celery

sudo pip install Celery -->(A command for installing Celery module in python)

Make a celery.py file

After installing your preferred broker, you may configure the settings in celery.py. I used MySQL in my situation. Here is an illustration of how the file will appear.

# Change this to your settings in celery.py

BROKER_URL = 'sqla+mysql://user:user_pass@localhost/dbname'

CELERY_RESULT_BACKEND = "database"

CELERY_RESULT_DBURI = 'mysql://user_pass@localhost/dbname'

Write Any Tasks

Set up your tasks (generally they will be saved into a file called tasks.py). What my tasks.py looks like is as follows:

from celery import Celery

celery = Celery('tasks')

celery.config_from_object('celery')

@celery.task

def addition(first_arg, second_arg):

return first_arg + second_arg

Modifying The celery.py File

Then, Modify the celery.py to include the following information so Celery will know how frequently you want to perform your periodic tasks.

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {

'every-minute': {

'task': 'tasks.addition',

'schedule': crontab(), # runs every minute

'args': (8,10),

},

}

Initiating Celery By command In Linux

Our task will now execute every minute if we only start a Celery worker with the —beat flag. The —beat flag must appear after the worker in order for anything to happen; this element was not immediately apparent.

$ celery -A tasks worker --loglevel=info --beat

Final output for which we are curious will look like this :

As jobs are completed, Celery should output a lot of information. You should spot a line that reads something like this among them:

[2022-08-28 13:26:00,343: INFO/MainProcess] Task tasks.addition[444675955


We are an ERP software development company that builds custom enterprise solutions from scratch for diverse business needs. Our seasoned developers use open-source software platforms like Odoo, OFBiz, and ERPNext to build scalable ERP applications with custom features. To learn more about our ERP development services, contact us at [email protected].