Django2.1 + Redis + celery + flower

刚开始用djcelery + rabbitmq 组合,但有时添加到其他app的tasks 会执行不了,不想把时间浪费在这些不稳定因素上,所以改用celery + redis
以下centos7.2、python3.6

一、安装,由于本系统存在py2,py3 ,所以用yum安装redis后,程序会报找不到模块的错误,类似以下链接:

https://github.com/liuliqiang/celerybeatredis/issues/13
大概的意思是用yum安装是装在了py2上,但程序是用py3运行的。

pip install redis
pip install celery
pip install flower

二、redis配置:

vim /etc/redis.conf
## 如下
bind 127.0.0.1
port 6379
daemonize yes
# requirepass foobared
...

redis 命令

service redis start
service redis stop
service redis restart
service redis status

三、django

在mysite/mysite/ 路径增加celery.py 文件,内容如下:

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

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

app = Celery('mysite')

# 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')
# platforms.C_FORCE_ROOT=True

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

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']

settings.py

CELERY_BROKER_URL = 'redis://127.0.0.1:6379/'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Shanghai'

最后配置tasks.py, 在你的app目录下增加tasks.py 文件

from __future__ import absolute_import
from celery import shared_task
import time

@shared_task
def test(x):
    time.sleep(x)

views.py 调用

from app.tasks import test

···
test.delay(5)
···

执行

# 先启动系统
python manage.py runserver
# 启动worker
celery -A mysite worker -l info
# 启动flower,查看任务执行情况
celery -A flower -l info --persistent=True

问题:
celery默认安装到/usr/local/python3/bin/celery路径,此路径不再$PATH中,所以shell找不到此路径,做一个连接就可以 ln -s /usr/local/python3/bin/celery /usr/bin/celery
有些环境安装了python2.x python3.x, 在启动celery的时候用的是python2去执行,要想使用python3执行,使用绝对路径即可。

你可能感兴趣的:(Django2.1 + Redis + celery + flower)