一、安装流程
1、pip install 安装如下包
Django==4.1
django-celery-beat==2.5.0
django-celery-results==2.5.1
redis==5.0.0
2、在project.settings.py文件里配置模块
'django_celery_results',
'django_celery_beat',
image.png
3、配置celery:
# django时区配置
TIME_ZONE = 'Asia/Shanghai'
# 如果USE_TZ设置为True时,Django会使用系统默认设置的时区,此时的TIME_ZONE不管有没有设置都不起作用
# 如果USE_TZ 设置为False,TIME_ZONE = 'Asia/Shanghai', 则使用上海的UTC时间。
USE_TZ = False
INSTALLED_APPS = (
...,
'django_celery_beat',
)
# celery beat配置
# CELERY_ENABLE_UTC = False
CELERY_TIMEZONE = TIME_ZONE
DJANGO_CELERY_BEAT_TZ_AWARE = False
CELERY_BEAT_SCHEDULER = 'django-celery-beat.schedulers.DatabaseScheduler'
# celery 的启动工作数量设置
CELERY_WORKER_CONCURRENCY = 10
# 任务预取功能,会尽量多拿 n 个,以保证获取的通讯成本可以压缩。
CELERYD_PREFETCH_MULTIPLIER = 20
# 有些情况下可以防止死锁
CELERYD_FORCE_EXECV = True
# celery 的 worker 执行多少个任务后进行重启操作
CELERY_WORKER_MAX_TASKS_PER_CHILD = 100
# 禁用所有速度限制,如果网络资源有限,不建议开足马力。
CELERY_DISABLE_RATE_LIMITS = True
# 设置代理人broker
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/2'
# 指定 Backend
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/1'
4、执行数据库python manage.py migrate,迁移之后会生成相应的表
5、在project文件下新增celery.py文件
import os
from celery import Celery, platforms
from celery.schedules import crontab
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_server.settings")
app = Celery("django_server")
app.config_from_object("django.conf:settings", namespace="CELERY")
# 定时任务的存放位置
app.autodiscover_tasks()
5、在某个模块下新增tasks.py文件,编辑测试内容如下:
from celery import shared_task
@shared_task(name='测试')
def add(x=1, y=2):
print("test add ######")
return x + y
@shared_task(name='test_mul')
def mul(x=2, y=3):
print("test add ######")
return x * y
6、注意:celery.py不会被框架自动加载进来,这里我们通过工程的init.py来把celery引进来 。
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
7、执行相关命令并测试:
命令行启动celery worker 的worker进程,用于消费redis队列(异步任务,定时任务):
celery -A your_project worker -l info
命令行启动celery beat的beat进程,用于往redis队列传输相应定时任务:
celery -A your_project beat -l info
二、问题反馈
1、执行celery -A project worker -l info命令报错:
class PrefixedStrictRedis(GlobalKeyPrefixMixin, redis.Redis):
AttributeError: 'NoneType' object has no attribute 'Redis'
解决方式:虚拟环境下执行pip install redis
2、执行celery -A project worker -l info命令报错:
image.png
解决方式:在某个模块下新增tasks.py文件,新增下面的内容保存即可
from celery import shared_task
@shared_task(name='测试')
def add(x, y):
print("test add ######")
return x + y
3、执行celery -A project beat -l info命令报错:
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django-celery-beat'
解决办法:
配置文件里CELERY_BEAT_SCHEDULER = 'django-celery-beat.schedulers.DatabaseScheduler'写错了,是django_celery_beat下划线,不是连字符
网友评论