本文介绍了Celery计划任务中的打印语句未出现在终端中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 celery -A task2.celery worker -B 时,我想看到每秒打印一次"celery task".当前没有任何打印.为什么这行不通?

When I run celery -A tasks2.celery worker -B I want to see "celery task" printed every second. Currently nothing is printed. Why isn't this working?

from app import app
from celery import Celery
from datetime import timedelta

celery = Celery(app.name, broker='amqp://guest:@localhost/', backend='amqp://guest:@localhost/')
celery.conf.update(CELERY_TASK_RESULT_EXPIRES=3600,)

@celery.task
def add(x, y):
    print "celery task"
    return x + y

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks2.add',
        'schedule': timedelta(seconds=1),
        'args': (16, 16)
    },
}

这是凝视工人并殴打之后的唯一输出:

This is the only output after staring the worker and beat:

[tasks]
  . tasks2.add

[INFO/Beat] beat: Starting...
[INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[INFO/MainProcess] mingle: searching for neighbors
[INFO/MainProcess] mingle: all alone

推荐答案

您已编写了时间表,但未将其添加到celery配置中.因此Beat认为没有计划发送的任务.下面的示例使用 celery.config_from_object(__ name __)从当前模块中获取配置值,但是您也可以使用任何其他配置方法.

You wrote the schedule, but didn't add it to the celery config. So beat saw no scheduled tasks to send. The example below uses celery.config_from_object(__name__) to pick up config values from the current module, but you can use any other config method as well.

配置正确后,您将看到节拍中有关发送计划任务的消息,以及工作人员接收并运行这些任务时这些任务的输出.

Once you configure it properly, you will see messages from beat about sending scheduled tasks, as well as the output from those tasks as the worker receives and runs them.

from celery import Celery
from datetime import timedelta

celery = Celery(__name__)
celery.config_from_object(__name__)

@celery.task
def say_hello():
    print('Hello, World!')

CELERYBEAT_SCHEDULE = {
    'every-second': {
        'task': 'example.say_hello',
        'schedule': timedelta(seconds=5),
    },
}
$ celery -A example.celery worker -B -l info

[tasks]
  . example.say_hello

[2015-07-15 08:23:54,350: INFO/Beat] beat: Starting...
[2015-07-15 08:23:54,366: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2015-07-15 08:23:54,377: INFO/MainProcess] mingle: searching for neighbors
[2015-07-15 08:23:55,385: INFO/MainProcess] mingle: all alone
[2015-07-15 08:23:55,411: WARNING/MainProcess] celery@netsec-ast-15 ready.
[2015-07-15 08:23:59,471: INFO/Beat] Scheduler: Sending due task every-second (example.say_hello)
[2015-07-15 08:23:59,481: INFO/MainProcess] Received task: example.say_hello[2a9d31cb-fe11-47c8-9aa2-51690d47c007]
[2015-07-15 08:23:59,483: WARNING/Worker-3] Hello, World!
[2015-07-15 08:23:59,484: INFO/MainProcess] Task example.say_hello[2a9d31cb-fe11-47c8-9aa2-51690d47c007] succeeded in 0.0012782540870830417s: None

这篇关于Celery计划任务中的打印语句未出现在终端中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 12:38