我的__init__.py
文件
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
我的celery.py
文件
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
我的/etc/supervisor/conf.d/celery.conf
文件:
[program:]
command=/home/user/venv/bin/celery -A myproject worker --loglevel=INFO
environment=PYTHONPATH=/home/user/project-folder
user=user
numprocs=1
autostart=true
autorestart=true
stdout_logfile=/home/user/logs/celery.log
stderr_logfile=/home/user/logs/celery.log
startssecs=10
stopwaitsecs = 600
stopasgroup=true
priority=100
这是错误消息的最后一部分celery.log
File "/home/user/venv/lib/python3.6/site-packages/celery/loaders/base.py", line 131, in config_from_object
self._conf = force_mapping(obj)
File "/home/user/venv/lib/python3.6/site-packages/celery/utils/collections.py", line 48, in force_mapping
return DictAttribute(m) if not isinstance(m, Mapping) else m
File "/home/user/venv/lib/python3.6/abc.py", line 183, in __instancecheck__
subclass = instance.__class__
File "/home/user/venv/lib/python3.6/site- packages/django/utils/functional.py", line 215, in inner
self._setup()
File "/home/user/venv/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/home/user/venv/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/user/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 951, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 894, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1157, in find_spec
File "<frozen importlib._bootstrap_external>", line 1129, in _get_spec
File "<frozen importlib._bootstrap_external>", line 1273, in find_spec
File "<frozen importlib._bootstrap_external>", line 1231, in _get_spec
File "<frozen importlib._bootstrap_external>", line 556, in
spec_from_file_location
RecursionError: maximum recursion depth exceeded
我不知道这个配置有什么问题?
当我删除以下句子时,我也有同样的问题
app.config_from_object('django.conf:settings', namespace='CELERY')
然后它起作用了。所以我认为问题是因为这个原因而发生的。 也许我们应该指定这个函数的配置。
from django.conf import settings
app.config_from_object(settings, namespace='CELERY')
实际上,这个settings
进去。
您不应该在__init__.py
文件中导入您的芹菜应用程序。 把它拿出来,它将修复你的无限启动导入循环。 从本质上讲,您的芹菜应用程序启动了 django 设置模块初始化,该模块反过来加载应用程序并尝试重新加载加载您的芹菜文件的项目__init__.py
文件,然后尝试重新加载设置文件,因为它尚未初始化,这反过来......(你明白了(。
如果它对某人有帮助 - 我遇到了类似的问题,它是由Celery
类的覆盖on_configure
方法中的异常引起的。我想任何阻止该方法正常执行Celery._load_config
都可能导致"超出最大递归深度"错误。
我尝试评论该行
app.config_from_object('django.conf:settings', namespace='CELERY'(
并且错误消失了。当在 settings.py 中找不到与芹菜相关的设置时,它会给我此错误。之后,我添加
CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379'
它再次起作用。我正在使用 docker compose 来设置 django + redis + celery。我想它无法在没有任何芹菜设置的情况下检测到 settings.py 文件