基於Django1.10與Celery4實現異步隊列任務

由於django-celery這個模塊我在django1.10的版本內運行不起來,只能使用野生的Celery,Celery4支持django1.8以上的版本,1.8以下的版本請使用Celery3,整個配置過程並不複雜:


整個目錄結構:

wKioL1iBywTB1qJAAAAXihfUleg838.png-wh_50


一、安裝模塊:

pip install celery
pip install django-celery-results

django-celery-results作用是將Celery的運行結果存入數據庫


二、建立Celery入口文件(celery.py):

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

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

app = Celery('Mir2Admin')

# Using a string here means the worker don'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')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


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


三、把celery.py中的app加入__init__.py文件中,確保django運行的時候加載到它:

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 .celery import app as celery_app

__all__ = ['celery_app']


四、配置settting.py:

# celery配置
CELERY_BROKER_URL = 'redis://:[email protected]:6379/3'

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_BACKEND = 'django-db'
CELERY_TASK_SERIALIZER = 'json'

INSTALLED_APPS = [
    ......
    'django_celery_results',
    ......
]

我的broker用redis,沒有redis的google擼一個

CELERY_RESULT_BACKEND這一項只有裝了django-celery-results這個模塊這裏才能配置'django-db',把結果存入數據庫


五、生成數據表:

migrate django_celery_results


六、在自己的app中建立tasks.py文件,添加需要異步執行的函數:

# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)


五、啓動測試(在django工程的manage.py同層目錄執行):

/usr/local/python27/bin/celery -A Mir2Admin worker -l info


六、簡單測試:

python manage.py shell    
>>> from myapp.tasks import add    
>>> add.delay(2, 2)

從第五步的前臺日誌可以看到Celery執行結果,django-celery-results會把結果存進數據庫

wKioL1iBz9yB7ZEfAABFkTb1oaI012.png-wh_50


七、生產環境配置(附件):


添加啓動腳本:

chmod +x /etc/init.d/celeryd


添加配置文(/etc/default/celeryd):

# Names of nodes to start
#   most people will only start one node:
CELERYD_NODES="Mir2AdminCelery"
#   but you can also start multiple and configure settings
#   for each in CELERYD_OPTS
#CELERYD_NODES="worker1 worker2 worker3"
#   alternatively, you can specify the number of nodes to start:
#CELERYD_NODES=10

# Absolute or relative path to the 'celery' command:
CELERY_BIN="/usr/local/python27/bin/celery"

# App instance to use
# comment out this line if you don't use an app
CELERY_APP="Mir2Admin"
# or fully qualified:
#CELERY_APP="proj.tasks:app"

# Where to chdir at start.
CELERYD_CHDIR="/data/www/Mir2Admin"

# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# Configure node-specific settings by appending node name to arguments:
#CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"

# Set logging level to DEBUG
#CELERYD_LOG_LEVEL="DEBUG"

# %n will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/data/www/Mir2Admin/logs/%n%I.log"
CELERYD_PID_FILE="/data/www/Mir2Admin/%n.pid"

# 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="root"
CELERYD_GROUP="root"

# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1

CELERYD_NODES:名稱,影響日誌文件名及pid文件名

CELERY_BIN:celery路徑

CELERY_APP:django工程名稱

CELERYD_CHDIR:django工程路徑

CELERYD_OPTS:參數

CELERYD_LOG_FILE:日誌路徑

CELERYD_PID_FILE:PID文件路徑


最後啓動一發:

/etc/init.d/celeryd start



參考文章:http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章