celery使用
Windows执行task的时候出错,所以在linux执行
操作步骤
1 安装celery(博客是用4.1.1本版),redis(或者RabbitMQ)
2 编写task
tasks.py
from celery import Celery
# app = Celery(main='tasks', broker='redis://localhost:6379/0', backend='redis://localhost:6379/0')
app = Celery(main='tasks')
app.config_from_object('celeryconfig') # 从外边引入配置
@app.task
def add(x, y):
return x + y
celeryconfig.py (与tasks.py在同级目录下)
broker_url = 'redis://localhost:6379/0'
result_backend = 'redis://localhost:6379/1'
task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
timezone = 'Europe/Oslo'
enable_utc = True
3 运行worker
在tasks.py目录下执行 celery -A tasks worker --loglevel=info
出现以下内容 说明运行成功
4 调用任务
from tasks import add
r = add.delay(3, 3)
------------------------------------------------
* delay() 是 apply_async() 的快捷方法
* r.reday() 检测是否已经处理完毕
* result.get(timeout=1) 获取结果
* r.traceback 对异常回溯
新开一个终端,获取结果
一个博客
最基本使用:
使用参数解析:
进阶使用(与使用参数解析这个博客结合看):
django使用celery(与最基本使用博客结合使用):