【发布时间】:2017-11-14 03:25:22
【问题描述】:
我已将 Django 从 1.7 版升级到 1.11.2 版,将 Celery 从 3.1.25 版升级到 4.0.1 版。
在此升级之前,我能够以这种方式运行 celery workers throw django-celery 包:
python manage.py celery worker -Q <my_queue> --app=proj.celery:app --concurrency=5
阅读 Celery 发行说明和文档,我发现使用 Django ORM 作为结果后端,我不能再使用 django-celery 包,但我必须使用 django-celery-results 和 django-celery-beat,而不是它。
使用相同的命令我不能再运行工人了。
更详细地说,我尝试运行以下命令:
celery worker -app=proj.celery:app -l info
其中 celery 是我的 virtualenv 中的 celery.exe 文件 (virtual_env_name\Scripts\celery.exe)。
这引发了以下错误:
Traceback (most recent call last):
File "c:\python27\Lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\virtualenv\<virtual_env_name>\Scripts\celery.exe\__main__.py", line 5, in <module>
File "path\of\my\project\celery.py", line 8, in <module>
from celery import Celery
ImportError: cannot import name Celery
我尝试直接从命令行导入 celery,效果很好。运行命令时出现问题
celery.py 文件
from __future__ import absolute_import
import os
from celery import Celery
import django
django.setup()
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
项目结构
- main
- proj
- __init__.py
- setting.py
- celery.py
- manage.py
我正在运行 Python2.7 和 Windows 32。
提前致谢
【问题讨论】:
标签: python django python-2.7 celery