badass django with celery 🚬

pip install celery
pip show celery
Name: celery
Version: 4.4.6
Summary: Distributed Task Queue.
Home-page: http://celeryproject.org
Author: Ask Solem
Author-email: auvipy@gmail.com
License: BSD
Location: /home/my_pc/.local/lib/python3.6/site-packages
Requires: vine, billiard, kombu, pytz, future
Required-by:
my_app/
|-- my_app/
| |-- core/
| | |-- migrations/
| | |-- templates/
| | |-- apps.py
| | |-- models.py
| | +-- views.py
| |-- templates/
| |-- __init__.py
| |-- settings.py
| |-- celery.py <-- celery file goes here
| |-- urls.py
| +-- wsgi.py
|-- manage.py
+-- requirements.txt
import os from celery import Celery
from os.path import dirname, basename
# To set which setting you want to use by default
os.environ.setdefault('DJANGO_SETTINGS_MODULE','my_app.settings')
# to initalize our Celery app
app = Celery(basename(dirname(__file__)))
# to inform celery load settings which are specific to celery
app.config_from_object('django.conf:settings', namespace='CELERY')
# to tell Celery that it should use the settings.INSTALLED_APPS list of applications to find defined Celery tasks.app.autodiscover_tasks()
my_app/
|-- my_app/
| |-- core/
| | |-- migrations/
| | |-- templates/
| | |-- apps.py
| | |-- models.py
| | +-- views.py
| |-- templates/
| |-- __init__.py <-- edit this init file
| |-- settings.py
| |-- celery.py
| |-- urls.py
| +-- wsgi.py
|-- manage.py
+-- requirements.txt
from .celery import app as celery_app  # This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
__all__ = ['celery_app']
my_app/
|-- my_app/
| |-- core/
| | |-- migrations/
| | |-- templates/
| | |-- apps.py
| | |-- tasks.py <-- create this file here
| | |-- models.py
| | +-- views.py
| |-- templates/
| |-- __init__.py
| |-- settings.py
| |-- celery.py
| |-- urls.py
| +-- wsgi.py
|-- manage.py
+-- requirements.txt
# change import here according to your project name
from my_app.celery import app # <-- ⚡⚡⚡⚡⚡⚡⚡⚡ change here
from .models import User
import random
@app.task()
def update_user():
dummy = str(random.randint(100, 1000))
User.objects.filter(id=1).update(username=dummy)
my_app/
|-- my_app/
| |-- core/
| | |-- migrations/
| | |-- templates/
| | |-- apps.py
| | |-- tasks.py
| | |-- models.py
| | +-- views.py
| |-- templates/
| |-- __init__.py
| |-- settings.py
| |-- celery.py
| |-- celery_config.py <-- create your file here
| |-- urls.py
| +-- wsgi.py
|-- manage.py
+-- requirements.txt
CELERY_BEAT_SCHEDULE = {
# name for your scheduler
'update_my_user': {
# path for your task
'task': 'my_app.core.tasks.update_user',
# 10 defined here is in seconds
'schedule': 10,
},
}
from .celery_config import *
redis-server
redis-cli ping
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_TIMEZONE = 'UTC'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
celery -A my_app worker -l info
  • -A denote for app name which is added next to it in my case it is my_app, change accordingly your app name
  • worker is what we want to run
  • -l denote level of log we want, we are keeping this as info
celery -A my_app beat -l info

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aR chaudhary

aR chaudhary

Senior Software Engineer 👨‍💻