9

我收到以下错误:

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 应用程序。因此,我没有创建项目或应用程序启动时创建的一个wsgi.pymodels.py任何典型文件。django-admin

我只想用来djcelery创建周期性任务djcelery.schedules.DatabaseScheduler

此处给出的问题的解决方案(AppRegistryNotReady,使用 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()
  • 芹菜配置文件
    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'),
        }
    }

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

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

4

1 回答 1

0

As it is said in django documentation, if you run your django app as a standalone one, you should ensure all the steps of the initialization process to be done by yourself:

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.

In your case it would be is as simple as call 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')

Also, your settings module (celeryconfig.py) should be treated as a python code, so you have to import all of the used objects (Queue and Exchange from kombu and the os module).

And contain django-required settings. Add a SECRET_KEY in your celeryconfig.py file:

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'),
    }
}
于 2019-02-20T20:37:41.137 回答