jjzjj

python - Django/djcelery 1.8.2 AppRegistryNotReady : Translation infrastructure cannot be initialized

coder 2023-08-22 原文

我收到以下错误:

File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/utils/translation/trans_real.py", line 164, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

我有一个项目,它不是一个真正的 Django 应用程序,而是一个 celery 应用程序。因此,我没有在项目或应用程序已启动。

我只想使用 djcelery 来使用 djcelery.schedules.DatabaseScheduler 创建周期性任务,就像这里指定的一样 Add, modify, remove celery.schedules at run time在这里 How to dynamically add / remove periodic tasks to Celery (celerybeat)

此处给出的问题解决方案 (AppRegistryNotReady, translation bug when deploying with uWSGI) 要求我对 vassal.ini 文件进行更改。我的实现中没有 vassal.ini 文件。

我将简要描述我的项目-

proj
  apps.py
  tasks.py
  celeryconfig.py
  runproj.py
  • 应用程序.py
    from celery import Celery
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celeryconfig')
    myapp = Celery('myapp')
    myapp.config_from_object('celeryconfig')
    if __name__ == '__main__'
        myapp.worker_main('-B', '-S', 'djcelery.schedules.DatabaseScheduler')
  • 任务.py
    from apps import myapp
    @myapp.task(name='msgprinter')
    def msg_printer(msg):
        print msg
  • 运行项目.py
    from djcelery.models import PeriodicTask, IntervalSchedule
    intSch = IntervalSchedule(period='seconds', every=30)
    periodic_task = PeriodicTask(
      name = 'msg_printer_schedule',
      task = 'proj.tasks.msg_printer',
      interval = intSch,
      args=json.dump(['such wow']),
     )
    periodic_task.save()
  • celeryconfig.py
    CELERY_ACCEPT_CONTENT = ['pickle', 'json']
    BROKER_URL = 'amqp://guest@localhost'
    CELERY_IMPORTS = ('proj.tasks')
    CELERY_QUEUES = [Queue('default', Exchange('default', type='direct'), routing_key='default')]

    #DJANGO SETTINGS
    INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djcelery',
    'mypp')

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join('/home', 'test.db'),
        }
    }

在运行 worker 之前,我使用 django-admin migrate 命令创建了所需的表。我可以在 /home/test.db 数据库中看到这些表。

首先我运行 workers - $python apps.py 然后我将计划保存到数据库中,由 celerybeat 守护进程重复执行 - $python runproj.py

最佳答案

正如 django 文档中所述,如果您将 django 应用程序作为独立应用程序运行,则应确保 initialization process 的所有步骤自己完成:

setup()

This function is called automatically:

  • When running an HTTP server via Django’s WSGI support.

  • When invoking a management command.

It must be called explicitly in other cases, for instance in plain Python scripts.

在你的情况下,它就像调用 django.setup() 一样简单:

from celery import Celery
import django
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celeryconfig')
myapp = Celery('myapp')
myapp.config_from_object('celeryconfig')

if __name__ == '__main__'
    django.setup()
    myapp.worker_main('-B', '-S', 'djcelery.schedules.DatabaseScheduler')

此外,您的设置模块 (celeryconfig.py) 应被视为 python 代码,因此您必须导入所有使用的对象(Queue >从 kombuos 模块交换

并包含 django 所需的设置。在您的 celeryconfig.py 文件中添加一个 SECRET_KEY:

from kombu import Exchange, Queue
import os


CELERY_ACCEPT_CONTENT = ['pickle', 'json']
BROKER_URL = 'amqp://guest@localhost'
CELERY_IMPORTS = ('proj.tasks')
CELERY_QUEUES = [Queue('default', Exchange('default', type='direct'), routing_key='default')]

#DJANGO SETTINGS
SECRET_KEY = "YOUR_TOP_SECRET_KEY"

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djcelery',
'mypp')

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join('/home', 'test.db'),
    }
}

关于python - Django/djcelery 1.8.2 AppRegistryNotReady : Translation infrastructure cannot be initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30791795/

有关python - Django/djcelery 1.8.2 AppRegistryNotReady : Translation infrastructure cannot be initialized的更多相关文章

随机推荐