Skip to content

Instantly share code, notes, and snippets.

@brizandrew
Created August 15, 2018 21:29
Show Gist options
  • Save brizandrew/ccc4fccbd0acca71af7e6d934d6c3896 to your computer and use it in GitHub Desktop.
Save brizandrew/ccc4fccbd0acca71af7e6d934d6c3896 to your computer and use it in GitHub Desktop.
Guide to setting up celery.

Setting Up Celery

Boilerplate Code

Install celery and redis

$ pipenv install celery redis

Configure the celery.py file in your exampleapp folder with your app name.

# example/exampleapp/celery.py
import os

from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exampleapp.settings')

app = Celery('yourcustomappname', broker="redis://localhost")
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.update(
    task_serializer='json',
    timezone='America/New_York',
)
# Use synchronous in local dev
if settings.DEBUG:
    app.conf.update(task_always_eager=True)
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, related_name='celery')

Configure the __init__.py file in your exampleapp folder to begin using celery.

# example/exampleapp/__init__.py
from .celery import app as celery_app

__all__ = ['celery_app']

Add tasks in your app's tasks folder and give it a shared_task decorator.

# yourapp/tasks/test_task.py
@shared_task(acks_late=True)
def test_task(data):
    logger.info(data)

Import your tasks in your app's celery.py file.

# yourapp/celery.py
from .tasks.test_task import test_task

Import your celery task inside a file and call it with .delay()

# yourapp/some_file.py
from yourapp.celery import test_task

test_task.delay('test')

Starting A Worker

To make sure your app works using a worker, comment out the section that turns on task_always_eager in your exampleapp's celery.py.

Start you redis server.

$ redis-server

Start your celery worker from within your example directory (where manage.py is).

$ celery -A exampleapp worker -l info

You should see tasks being completed in your celery worker log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment