Celery - Tirrilee/TechTalk GitHub Wiki
Pythonμμ λΉλκΈ° λμμ μν΄ μ¬μ©νλ λΌμ΄λΈλ¬λ¦¬
Celery version 4.3 runs on, Python (2.7, 3.4, 3.5, 3.6, 3.7), PyPy2.7 (6.0), PyPy3.5 (6.0)
Celery 4.0 supports Django 1.8 and newer versions. Please use Celery 3.1 for versions older than Django 1.8
$ pip install celery==4.3.0
$ brew install redis
- Django Project
- project
- settings
- local
- dev
- prod
νμΌ κ²½λ‘κ° μ€μν©λλ€!!
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from celery import Celery
import os
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.dev')
app = Celery('project', broker=settings.CELERY_BROKER_URL) # νλ‘μ νΈ μ΄λ¦ & broker μ€μ νμ§ μμΌλ©΄ μλ¨
# 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', namespace='CELERY') # Version 4 μ΄μ
# Load task modules from all registered Django app configs.
# a common practice for reusable apps is to define all tasks in a separate tasks.py module,
# and Celery does have a way to auto-discover these modules
app.autodiscover_tasks() # Version 4 μ΄μfrom __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .settings.celery import app as celery_app
__all__ = ('celery_app',)from __future__ import absolute_import
from project.settings.celery import app
@app.task
def say_hello():
print("hellow world!")Shellμ μ€ννλ κ²½λ‘λ projectμ μμ λ¨κ³μ directoryμμ μ€ν
$ celery -A project worker -l info
$ sudo apt-get install redis-server
$ sudo systemctl start redis
$ sudo vi /etc/init.d/celeryd
celery repo 볡λΆ
$ sudo vi /etc/default/celeryd
# Names of nodes to start
# most people will only start one node:
CELERYD_NODES=1
# Absolute or relative path to the 'celery' command:
CELERY_BIN="/home/ubuntu/movedot/venv/bin/celery"
# App instance to use
# comment out this line if you don't use an app
CELERY_APP="project"
# Where to chdir at start.
CELERYD_CHDIR="/home/ubuntu/movedot/api-v1/"
# CELERYD_MULTI="$CELERYD_CHDIR/manage.py celeryd_multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# CELERYD_MULTI="multi"
# CELERY_CONFIG_MODULE="project.settings.dev"
# %N will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%N.log"
CELERYD_PID_FILE="/var/run/celery/%N.pid"
CELERYD_LOG_LEVEL="INFO"
# Workers should run as an unprivileged user.
# You need to create this user manually (or you can choose
# a user/group combination that already exists, e.g. nobody).
CELERYD_USER="celery"
CELERYD_GROUP="celery"
# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1
export DJANGO_SETTINGS_MODULE="project.settings.dev"
export PYTHONPATH="/home/ubuntu/movedot/venv/bin/python"
$ sudo chmod 755 /etc/init.d/celeryd
$ sudo chown root:root /etc/init.d/celeryd
$ sudo /etc/init.d/celeryd start
$ sudo sh -x /etc/init.d/celeryd start : celery history
μ€λ₯! > Permission μ€λ₯λ logμ 보μ΄μ§ μλλ€
$ sudo su
$ C_FAKEFORK=1 sh -x /etc/init.d/celeryd start