[PYTHON] Celery notes on Django


Installation

$ pip install django
$ pip install django-celery
$ pip freeze
amqp==1.4.8
anyjson==0.3.3
billiard==3.3.0.22
celery==3.1.19
Django==1.9
django-celery==3.1.17
kombu==3.0.30
pytz==2015.7
wheel==0.24.0

Django settings

1 Creating a project

$ django-admin.py startproject proj
$ python manage.py startapp sampleapp
/c/dev/projects/proj

Verification

|--manage.py
|--proj
| |--__init__.py
| |--settings.py
| |--urls.py
| |--wsgi.py
|--sampleapp
| |--__init__.py
| |--admin.py
| |--apps.py
| |--migrations
| | |--__init__.py
| |--models.py
| |--tests.py
| |--views.py

2 settings

/proj/settings.py

INSTALLED_APPS = (

    'djcelery',
    'kombu.transport.django',
    'sampleapp',
)

# Celery Setting
import djcelery
djcelery.setup_loader()
BROKER_URL = 'django://'
# Tasks will be executed asynchronously.
CELERY_ALWAYS_EAGER = False

/proj/celery.py

from __future__ import absolute_import

import os

from celery import Celery

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

from django.conf import settings  # noqa

app = Celery('proj')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)

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

/proj/init.py

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app  # noqa

3 Task implementation

/sampleapp/tasks.py

import time

from celery import task

@task
def add(a, b):
    time.sleep(10)
    return a + b

4 DB initialization

$  python manage.py makemigrations djcelery
Migrations for 'djcelery':
  0002_auto_20151211_1830.py:
    - Alter field status on taskmeta
    - Alter field state on taskstate

$  python manage.py migrate djcelery
Operations to perform:
  Apply all migrations: djcelery
Running migrations:
  Rendering model states... DONE
  Applying djcelery.0002_auto_20151211_1830... OK

Run

1 Start Celery worker

$ celery -A proj worker -l info -c 1

2 Run from Django shell

$  python manage.py shell
(InteractiveConsole)
>>> from sampleapp.tasks import add
>>> result = add.delay(1, 2)
>>> result.ready()
False
>>> result.ready()  #10 seconds have passed
True
>>> result.get()
3

Recommended Posts

Celery notes on Django
[Django] Notes on using django-debug-toolbar
django notes
Django notes
[Django] as_view () notes
Notes on Flask
Django Template notes
[Django] JWT notes
Notes on creating static files in Django
Notes on neural networks
Run Django on PythonAnywhere
Notes on installing PycURL
[Personal notes] Python, Django
Hello World on Django
Notes on using Alembic
Notes on SciPy.linalg functions
Miscellaneous notes about deploying the django app on Heroku
Notes on tf.function and Tracing
Notes on installing dlib on mac
Notes on python's sqlite3 module
Notes on * args and ** kargs
Notes on defining PySide slots (2)
Notes on pyenv and Atom
Notes on defining PySide slots
[Python] Notes on data analysis
Notes on optimization using Pytorch
Notes on installing Python on Mac
Django environment development on Windows 10
Notes on studying multidimensional scaling
Install Django on your Mac
Notes on installing pipenv on Mac
Hello World (beginners) on Django
Notes on installing Anaconda 3 on Windows
[Django] Directory structure practices + notes
Notes on imshow () in OpenCV
Notes on installing Python on CentOS
Launch notes for existing Django applications
Notes on package management with conda
Notes on using MeCab from Python
[Golang] Notes on frequently used functions
Notes on how to use pywinauto
Notes on using post-receive and post-merge
Deploy your Django application on Heroku
Django
Notes on how to use featuretools
python + django + scikit-learn + mecab (1) on heroku
Implement a Django app on Hy
Try Ajax on the Django page
python + django + scikit-learn + mecab (2) on heroku
Publish DJango page on heroku: Practice
Notes on installing Python using PyEnv
Django + v (irtual) env + Celery + Supervisor
Notes on using rstrip with python.
Notes on accessing dashDB from python
celery
Update applications running on Django 1.7 to Django 1.8
[Note] Run Django on Amazon Linux 2
Notes on how to use doctest
Django blog on heroku: login implementation
Notes on using matplotlib on the server
Notes on how to write requirements.txt