无法配置格式化程序"json":无法解析"app_name.utils.logging.JSONFormatter":无法导入名称"Celery"



我正试图将芹菜纳入我们的Django应用程序中,但在设置方面遇到了困难。到目前为止,我对stackoverflow/google.com的所有搜索都告诉我,我有一个循环依赖,但我看不到它,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设置

app_name/app_name_celery.py

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

此外,您不需要将芹菜导入到您的应用程序配置中。

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

相关内容

  • 没有找到相关文章

最新更新