我遇到了以下错误:
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.py
、models.py
或任何由django-admin
在启动项目或应用程序时创建的典型文件。
我只想使用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 -
proj
apps.py
tasks.py
celeryconfig.py
runproj.py
- apps.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')
- tasks.py
from apps import myapp
@myapp.task(name='msgprinter')
def msg_printer(msg):
print msg
- runproj.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'),
}
}
在运行工作进程之前,我使用
django-admin migrate
命令创建了所需的表。我可以在/home/test.db
数据库中看到这些表。首先,我运行工作进程 -
$python apps.py
然后,我将一个计划保存到数据库中,以便由celerybeat守护进程重复执行 - $python runproj.py