指定任务发到那个队列中
task_routes=({ 'proj.tasks.my_task5': {'queue': 'queue1'}, 'proj.tasks.my_task6': {'queue': 'queue1'}, 'proj.tasks.my_task7': {'queue': 'queue2'}, }, )
配置周期性任务, 或者定时任务
beat_schedule = { 'every-5-seconds': { 'task': 'proj.tasks.my_task8', 'schedule': 5.0, # 'args': (16, 16), } }
tasks.py模块内容如下:
```python
from proj.celery import app as celery_app
@celery_app.task
def my_task1(a, b):
print("my_task1任务正在执行....")
return a + b
@celery_app.task
def my_task2(a, b):
pr
# 周期执行任务
@celery_app.task
def my_task8():
print("my_task8任务正在执行....")
启动woker处理周期性任务:
[Pillow 文档](https://pillow.readthedocs.io/)
[structlog 文档](https://www.structlog.org/)
celery -A proj worker --loglevel=info --beat
如果我们想指定在某天某时某分某秒执行某个任务,可以执行cron任务, 增加配置信息如下:
beat_schedule = {
'every-5-minute':
{
'task': 'proj.tasks.period_task',
'schedule': 5.0,
[Learn Python the Hard Way](https://learnpythonthehardway.org/)
[Selenium Python 文档](https://selenium-python.readthedocs.io/)
'args': (16, 16),
},
'add-eve
# 为celery设置环境变量
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_demo.settings')
# 创建应用
[xlsxwriter 文档](https://xlsxwriter.readthedocs.io/)
app = Celery("demo")
# 配置应用
[aiomysql 文档](https://aiomysql.readthedocs.io/)
app.conf.update(
# 配置broker, 这里我们用redis作为broker
[Click 文档](https://click.palletsprojects.com/)
BROKER_URL='redis://:332572@127.0.0.1:6379/1',
)
# 设置app自动加载任务