gpt4 book ai didi

python - celery 中的简单周期性任务不起作用,但没有错误

转载 作者:行者123 更新时间:2023-11-30 22:48:11 24 4
gpt4 key购买 nike

我是 celery 新手。我正在尝试使用我的 Django 项目正确配置 Celery。为了测试 celery 是否工作,我创建了一个定期任务,该任务应该每 2 秒打印一次“periodic_task”。不幸的是它不起作用,但没有错误。

1已安装rabbitmq

2 Project/project/celery.py

from __future__ import absolute_import

import os

from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

from django.conf import settings # noqa

app = Celery('project')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def myfunc():
print 'periodic_task'

@app.task(bind=True)
def debudeg_task(self):
print('Request: {0!r}'.format(self.request))

3 项目/project/__init__.py

from __future__ import absolute_import

from .celery import app as celery_app

4 Settings.py

INSTALLED_APPS = [
'djcelery',
...]
...
...

CELERYBEAT_SCHEDULE = {
'schedule-name': {
'task': 'project.celery.myfunc', # We are going to create a email_sending_method later in this post.
'schedule': timedelta(seconds=2),
},
}

python manage.py之前,我运行celery -A project worker -l info

仍然看不到任何每 2 秒在控制台中打印的“periodic_task”...您知道该怎么做吗?

编辑 Celery 控制台:

 -------------- celery@Milwou_NB v3.1.23 (Cipater)
---- **** -----
--- * *** * -- Windows-8-6.2.9200
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: dolava:0x33d1350
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery


[tasks]
. project.celery.debudeg_task
. project.celery.myfunc

编辑:

将worker改为beat后,似乎有效。每 2 秒就会发生一些事情(更改为 5 秒),但我看不到任务的结果。 (我可以将任何内容放入 CELERYBEAT_SCHEDULE 中,即使是错误的路径,也不会引发任何错误..)

我将 myfunc 代码更改为:

@app.task(bind=True)
def myfunc():
# notifications.send_message_to_admin('sdaa','dsadasdsa')
with open('text.txt','a') as f:
f.write('sa')

但我在任何地方都看不到 text.txt。

> celery -A dolava beat -l info
celery beat v3.1.23 (Cipater) is starting.
__ - ... __ - _
Configuration ->
. broker -> amqp://guest:**@localhost:5672//
. loader -> celery.loaders.app.AppLoader
. scheduler -> djcelery.schedulers.DatabaseScheduler

. logfile -> [stderr]@%INFO
. maxinterval -> now (0s)
[2016-10-26 17:46:50,135: INFO/MainProcess] beat: Starting...
[2016-10-26 17:46:50,138: INFO/MainProcess] Writing entries...
[2016-10-26 17:46:51,433: INFO/MainProcess] DatabaseScheduler: Schedule changed.

[2016-10-26 17:46:51,433: INFO/MainProcess] Writing entries...
[2016-10-26 17:46:51,812: INFO/MainProcess] Scheduler: Sending due task schedule-name (dolava_app.tasks.myfunc)
[2016-10-26 17:46:51,864: INFO/MainProcess] Writing entries...
[2016-10-26 17:46:57,138: INFO/MainProcess] Scheduler: Sending due task schedule-name (dolava_app.tasks.myfunc)
[2016-10-26 17:47:02,230: INFO/MainProcess] Scheduler: Sending due task schedule-name (dolava_app.tasks.myfunc)

最佳答案

尝试运行

$ celery -A 项目节拍 -l 信息

关于python - celery 中的简单周期性任务不起作用,但没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40264448/

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