gpt4 book ai didi

celery autodiscover_tasks 不适用于 ValueError : Empty module name

转载 作者:行者123 更新时间:2023-12-03 17:47:53 25 4
gpt4 key购买 nike

我试图在我的应用程序中重用 autodiscover_tasks 函数而不使用 django,但它总是在我的 celery.py 中执行 autodiscover_tasks 时抛出 ValueError: Empty module name

consumer/
/__init__.py
/celeryapp/
/__init__.py
/celery.py
/celeryconfig.py
/test1/
/__init__.py
/tasks.py

我在消费者目录中执行 celery(/usr/bin/celery) 命令
$cd /path/to/consumer
$celery worker -A celeryapp.celery -l debug

celeryapp/celery.py:
from __future__ import absolute_import
from celery import Celery

app = Celery()
app.config_from_object('celeryapp.celeryconfig')
app.autodiscover_tasks(['test1'])

测试1/tasks.py:
from __future__ import absolute_import
from celeryapp.celery import app

@app.task
def add(x,y):
return x+y

错误信息:

$ celery worker -A celeryapp.celery -l debug
Traceback (most recent call last):
File "/usr/bin/celery", line 11, in <module>
sys.exit(main())
File "/usr/lib/python2.6/site-packages/celery/__main__.py", line 30, in main
main()
File "/usr/lib/python2.6/site-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/usr/lib/python2.6/site-packages/celery/bin/celery.py", line 769, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/lib/python2.6/site-packages/celery/bin/base.py", line 307, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/usr/lib/python2.6/site-packages/celery/bin/celery.py", line 761, in handle_argv
return self.execute(command, argv)
File "/usr/lib/python2.6/site-packages/celery/bin/celery.py", line 693, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/usr/lib/python2.6/site-packages/celery/bin/worker.py", line 179, in run_from_argv
return self(*args, **options)
File "/usr/lib/python2.6/site-packages/celery/bin/base.py", line 270, in __call__
ret = self.run(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/celery/bin/worker.py", line 212, in run
state_db=self.node_format(state_db, hostname), **kwargs
File "/usr/lib/python2.6/site-packages/celery/worker/__init__.py", line 95, in __init__
self.app.loader.init_worker()
File "/usr/lib/python2.6/site-packages/celery/loaders/base.py", line 128, in init_worker
self.import_default_modules()
File "/usr/lib/python2.6/site-packages/celery/loaders/base.py", line 116, in import_default_modules
signals.import_modules.send(sender=self.app)
File "/usr/lib/python2.6/site-packages/celery/utils/dispatch/signal.py", line 166, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/lib/python2.6/site-packages/amqp/utils.py", line 42, in __call__
self.set_error_state(exc)
File "/usr/lib/python2.6/site-packages/amqp/utils.py", line 39, in __call__
**dict(self.kwargs, **kwargs) if self.kwargs else kwargs
File "/usr/lib/python2.6/site-packages/celery/app/base.py", line 329, in _autodiscover_tasks
self.loader.autodiscover_tasks(packages, related_name)
File "/usr/lib/python2.6/site-packages/celery/loaders/base.py", line 251, in autodiscover_tasks
related_name) if mod)
File "/usr/lib/python2.6/site-packages/celery/loaders/base.py", line 272, in autodiscover_tasks
return [find_related_module(pkg, related_name) for pkg in packages]
File "/usr/lib/python2.6/site-packages/celery/loaders/base.py", line 290, in find_related_module
pkg_path = importlib.import_module(package).__path__
File "/usr/lib/python2.6/site-packages/importlib/__init__.py", line 37, in import_module
__import__(name)
ValueError: Empty module name

最佳答案

在 autodiscover_tasks 调用之前,您应该:
1,os.environ.setdefault("DJANGO_SETTINGS_MODULE","dj_gaomu.settings")
2、django.setup()

关于celery autodiscover_tasks 不适用于 ValueError : Empty module name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493344/

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