参考文档:Win10下Celery4.2.1基于redis的部署与错误
1.安装
1.1window 安装 Redis
由于 Redis 并没有官方支持 Windows,
下载方法1 https://github.com/MSOpenTech/redis 中下载 Redis 包。
下载方法2(我用的这个,我下载的msi可安装的): https://github.com/MicrosoftArchive/redis/releases
在cmd命令行窗口中输入 redis-server redis.windows.conf 启动redis
redis可视化工具安装: https://redisdesktop.com/download
1.2.安装好python后安装 Celery 相关库
# pip intall celery
# pip install celery-with-redis
2.配置\启动worker
2.1配置文件
首先是clelry的运行配置文件celeryconfig.py(当然你也可以从命令行传入)
#celeryconfig.py
BROKER_URL = 'redis://localhost:6379/0' #redis的配置,如果是rabbitmq就不是这个配置
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_REDIS_MAX_CONNECTIONS = 4
CELERYD_CONCURRENCY = 4
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 5} # 5min
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json'] # Ignore other content
CELERY_TIMEZONE = 'Asia/Shanghai'
CELERY_ENABLE_UTC = True
2.2 创建celery 任务列表,注册1个任务
创建一个Celery Application用来定义任务列表。实例化一个Celery对象app,然后通过@app.task 装饰器注册一个 task。
app.task函数来produce 任务到rabbitmq或者在redis 或者数据库。
#celery_worker.py
from celery import Celery
app = Celery('celery_work')
app.config_from_object('celeryconfig')
@app.task
def add(x, y):
return x + y
2.3运行 worker,启动Celery Worker服务,来开始监听并执行任务
1)在celery_worker.py文件里面启动一个