gpt4 book ai didi

python - Celery 计划任务中的打印语句不会出现在终端中

转载 作者:太空狗 更新时间:2023-10-29 17:28:41 38 4
gpt4 key购买 nike

当我运行 celery -A tasks2.celery worker -B 时,我想看到每秒打印一次“celery task”。当前没有打印任何内容。为什么这不起作用?

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)
},
}

这是盯着 worker 并击败之后的唯一输出:

[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__) 从当前模块中获取配置值,但您也可以使用任何其他配置方法。

正确配置后,您将看到来自 beat 的有关发送计划任务的消息,以及工作人员接收和运行这些任务时的输出。

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

关于python - Celery 计划任务中的打印语句不会出现在终端中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31414468/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com