Celery 4.0中的定期任务

编程入门 行业动态 更新时间:2024-10-23 10:20:28
本文介绍了Celery 4.0中的定期任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

据我所知,由于Celery 3.1装饰器 @periodic_task 被废弃.

As I know, since Celery 3.1 decorator @periodic_task is depricated.

因此,我尝试从celery docs 运行一个示例并无法意识到我在做什么错.

So I am trying to run an example from celery docs, and can't realise, what am I doing wrong.

我在 task_planner.py 中有以下代码:

from celery import Celery from kombu import Queue, Exchange class Config(object): CELERY_QUEUES = ( Queue( 'try', exchange=Exchange('try'), routing_key='try', ), ) celery = Celery('tasks', backend='redis://', broker='redis://localhost:6379/0') celery.config_from_object(Config) celery.conf.beat_schedule = { 'planner': { 'task': 'some_task', 'schedule': 5.0, }, } @celery.task(queue='try') def some_task(): print('Hooray')

当我运行: celery -A task_planner worker -l info -B 时,我仅收到以下信息: [2016-11-27 19:06:56,119:INFO/Beat]计划程序:每5秒钟发送一次适当的任务计划程序(some_task).

And when I run: celery -A task_planner worker -l info -B, I recieve only the following: [2016-11-27 19:06:56,119: INFO/Beat] Scheduler: Sending due task planner (some_task) every 5 sec.

但是我期望输出为'Hooray'.

But I am expecting the output 'Hooray'.

那么,我想念什么?

推荐答案

已找到解决方案.我有任务:

Have found the solution. I had the task:

@celery.task(queue='try') def some_task(): print('Hooray')

我打印了它的名字:

print(some_task)

有以下几点:

<@task: task_planner.some_task of tasks:0x7fceaaf5b9e8>

所以我只是在这里将任务的名称从 some_task 更改为 task_planner.some_task :

So I just changed the name of the task from some_task to task_planner.some_task here:

celery.conf.beat_schedule = { 'planner': { 'task': 'task_planner.some_task', 'schedule': 5.0, }, }

它奏效了!

[2016-11-29 10:09:57,697: WARNING/PoolWorker-3] Hooray

注意.您应运行带有worker的beat(如果任务与beat在同一模块中)和日志级别"info"以查看结果:

Note. You should run beat with worker (if task in the same module as beat) and loglevel 'info' in order to see the results:

celery -A task_planner worker -B -l info

更多推荐

Celery 4.0中的定期任务

本文发布于:2023-11-10 09:40:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Celery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!