【问题标题】:Unable to configure formatter 'json': Cannot resolve 'app_name.utils.logging.JSONFormatter': cannot import name 'Celery'无法配置格式化程序“json”:无法解析“app_name.utils.logging.JSONFormatter”:无法导入名称“Celery”
【发布时间】:2021-12-14 12:15:30
【问题描述】:

我正在尝试将 celery 包含到我们的 Django 应用程序中,并且正在努力进行设置。 到目前为止,我对 stackoverflow/google 的所有搜索都告诉我我有一个循环依赖,但我看不到它。文档https://docs.celeryproject.org/en/stable/getting-started/first-steps-with-celery.html 明确使用from celery import Celery

我已经定义: app_name/app_name_celery.py

from celery import signals, Celery
import os
from django.conf import settings
# disable celery logging so that it inherits from the configured root logger
@signals.setup_logging.connect
def setup_celery_logging(**kwargs):
    pass

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

# Create default Celery app
app = Celery('app_name')

# namespace='CELERY' means all celery-related configuration keys
# should be uppercased and have a `CELERY_` prefix in Django settings.
# https://docs.celeryproject.org/en/stable/userguide/configuration.html
app.config_from_object("django.conf:settings", namespace="CELERY")

# When we use the following in Django, it loads all the <appname>.tasks
# files and registers any tasks it finds in them. We can import the
# tasks files some other way if we prefer.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

另外,我已经定义了app_name/my_app_config.py

from django.apps import AppConfig


class MyAppConfig(AppConfig):
    # ...

    def ready(self):
        # Import celery app now that Django is mostly ready.
        # This initializes Celery and autodiscovers tasks
        import app_name.app_name_celery

最后,我已经添加到我的__init__.py

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .app_name import app as celery_app

__all__ = ('celery_app',)

此外,此 pr 的日志记录设置未更改,但这里是:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'json': {
            '()': 'app_name.utils.logging.JSONFormatter'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'json'
        }
    },
    'loggers': {
        'ddtrace': {
            'level': 'WARNING'
        }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console']
    }
}

任何人都可以看到循环依赖或我可能缺少的其他内容吗?

【问题讨论】:

    标签: django celery


    【解决方案1】:

    您正在尝试导入 django 设置

    app_name/app_name_celery.py

    from celery import signals, Celery
    import os
    from django.conf import settings # This line here
    

    此外,您无需将 celery 导入您的应用配置。

    自动发现在 app/tasks.py、app_2/tasks.py 等中查找任务。

    【讨论】:

    • 我尝试删除该行,删除应用程序配置,甚至将 celery 移动到我的生产需求文件中。我仍然得到同样的错误。 ``` 回溯(最近一次调用):文件“/usr/local/lib/python3.6/logging/config.py”,第 386 行,在解析中找到 = self.importer(used) 文件“/app/app_name /__init__.py",第 11 行,在 中 from .app_name_celery import app as celery_app 文件 "/app/app_name/app_name_celery.py",第 1 行,在 中来自 celery 导入信号,Celery ModuleNotFoundError: No module named '芹菜'```
    • 我将导入更改为import app_name_celery,现在它即将到来。谢谢。
    猜你喜欢
    • 2021-03-08
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 2020-10-10
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多