尝试在Django中调度函数print_hello((。但代码似乎不起作用。
这是Django项目的布局。只给出了与芹菜相关的文件。
- celery_test
- celery_test
- __init__.py
- celery.py
- tasks.py
_init_.py代码:
from .celery import app as celery_app
__all__ = ('celery_app',)
celery.py代码:
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_test.settings')
app = Celery('celery_test')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.schedule_beat = {
'print-hello-every-2-seconds': {
'task': 'tasks.print_hello',
'schedule': 2,
},
}
app.autodiscover_tasks()
tasks.py代码:
from celery import shared_task
@shared_task
def print_hello():
print("Hello celery...")
运行celery -A celery_test worker -l info
后,提示显示以下错误。
-------------- celery@########## v4.4.7 (cliffs)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2020-09-26 19:03:12
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: celery_test:0x2c34a320eb0
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
[2020-09-26 19:03:13,202: INFO/SpawnPoolWorker-1] child process 4856 calling self.run()
[2020-09-26 19:03:13,202: INFO/SpawnPoolWorker-3] child process 784 calling self.run()
[2020-09-26 19:03:13,211: INFO/SpawnPoolWorker-2] child process 8868 calling self.run()
[2020-09-26 19:03:13,220: INFO/SpawnPoolWorker-4] child process 4064 calling self.run()
[2020-09-26 19:03:14,922: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [WinError 10061] No connection could be made because the target machine actively refused it.
Trying again in 2.00 seconds... (1/100)
[2020-09-26 19:03:18,937: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [WinError 10061] No connection could be made because the target machine actively refused it.
Trying again in 4.00 seconds... (2/100)
安装了以下版本。
django 3.1.1
celery 4.4.7
django-celery-beat 2.0.0
我试着把芹菜的等级降到3.1.15。但django芹菜节拍2.0.0与此不兼容。
您正在运行RabbitMQ吗?-芹菜经纪人(您也可以选择使用Redis,一种常用的芹菜经纪人(。RabbitMQ的默认端口是5672。
为了参考,请查看您还可以在上面提到的存储库中找到@shared_task
的用法。