Celery 项目:多任务结构使用
Celery 项目:多任务结构使用
目录结构:
celery_project
├── celery_tasks
│ ├── celery.py # 必须这个名字
│ └── tasks1.py
│ └── tasks2.py
├── check_result.py # 检查结果
└── send_task.py # 添加任务
celery.py
from celery import Celery
# broker='redis://:appleyuchi@127.0.0.1:6379/0'
# -h 106.75.30.97 -p 8007
broker = 'redis://106.75.30.97:8007/1'
backend = 'redis://106.75.30.97:8007/2'
cel = Celery('celery_demo', broker=broker, backend=backend, include=['celery_tasks.task1',])
# 时区
cel.conf.timezone = 'Asia/Shanghai'
# 是否使用UTC
cel.conf.enable_utc = False
task1.py
import time
from .celery import cel
@cel.task
def test_celery(res):
time.sleep(5)
print(res)
return "test_celery任务结果:%s"%res
add_task.py
from celery_tasks.task1 import test_celery
# 立即告知celery去执行test_celery任务,并传入一个参数
result = test_celery.delay('第一个的执行')
print(result.id)
send_task.py
from celery.result import AsyncResult
from celery_tasks.celery import cel
async = AsyncResult(id='4ce34a23-aa25-47c7-9f80-11bd05f156b9', app=cel)
if async.successful():
result = async.get()
print(result)
elif async.failed():
print('执行失败')
elif async.status == 'PENDING':
print('任务等待中')
elif async.status == 'RETRY':
print('任务异常后重试')
elif async.status == 'STARTED':
print('任务正在执行')
启动celery
# 到celery_project文件夹下运行
celery worker -A celery_tasks -l info -P eventlet
windows:celery及redis版本
celery==3.1.25
redis==2.10.6
使用中报错:
celery报错:TypeError: 'tuple' object is not callable
原代码:
backend = 'redis://106.75.30.97:8007/2',
# python你跟我说逗号!!!???
# 去掉逗号,一切正常
使用redis和celery执行异步任务时报错AttributeError: 'str' object has no attribute 'items'
redis版本高了:以下为正确的版本:
celery==3.1.25
redis==2.10.6