celery
celery directory structure
myproject/proj
├── __init__.py
├── celery.py # 这个必须是celery.py这个名字
└── tasks.py # 这个不一定是tasks.py, 但是要和include的一致
test.py
celery.py
from __future__ import absolute_import # 使用绝对导入
from celery import Celery
app = Celery("proj",
broker="amqp://guest@localhost//",
backend="amqp",
include=["proj.tasks"]
)
app.conf.update(
CELERY_ROUTES={
"proj.tasks.add":{"queue":"hipri"},# 把add任务放入hipri队列
# 需要执行时指定队列 add.apply_async((2, 2), queue='hipri')
}
)
if __name__ == "__main__":
app.start()
Absolute reference
顺便说一下 from __future__ import absolute_import
-->这样以后: 局部的包将不能覆盖全局的包, 本地的包必须使用相对引用了
如上:
from celery import Celery 表示引用的全局的
from proj.celery import app 表示引用局部的
Start is executed in myproject directory, instead of under the proj
celery -A proj worker -Q hipri # 这个worker只处理hipri这个队列的任务
Start the terminal, switch to the root directory of the Django project, run:
# 启动worker
celery worker -A 项目名 -l info
# 启动beat
celery beat -A 项目名 -l info