I'm using django 1.3 with celery. I followed the instruction in http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html but old version django project structure is different, and it complains KeyError
, below is the demo project myproj structure
我使用的是django 1.3和芹菜。我遵循了http://celery.readthedocs.org/en/latest/django/firststeps-with-django.html,但老版本django项目结构是不同的,它抱怨KeyError,下面是演示项目myproj结构。
.
├── app1
│ ├── __init__.py
│ ├── models.py
│ ├── tasks.py
│ ├── tests.py
│ ├── views.py
├── __init__.py
├── manage.py
├── mycelery.py
├── settings.py
├── urls.py
When I send a task from the web, it failed KeyError: 'myproj.app1.tasks.add'
. However it's ok when I send a task from python manage.py shell
当我从web发送任务时,它失败了KeyError:“myprojects .app1.tasks.add”。但是,当我从python管理中发送任务时,它是可以的。py壳
>>> from app1.tasks import add
>>> result = add.apply_async(args=[1,2], countdown=30)
>>> result.ready() #wait for 30 seconds
True
>>> result.get()
3
Here is my code https://gist.github.com/dengshuan/a4adc7b690e101da0520
这里是我的代码https://gist.github.com/dengshuan/a4adc7b690e101da0520。
1 个解决方案
#1
0
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
will register tasks in app1 directory as app1.tasks.add
, tasks sent from shell will generate task with the same name. But tasks from web generates task named as myproj.app1.tasks.add
. So it complains KeyError
.
autodiscover_tasks(lambda: settings.INSTALLED_APPS)将在app1目录中作为app1任务注册任务。此外,来自shell的任务将以相同的名称生成任务。但是,来自web的任务会生成名为myprojects .app1.task .add的任务。所以KeyError抱怨道。
Changing from tasks import add
to from app1.tasks import add
solves the problem.
从任务导入添加到app1。任务导入添加解决了问题。
And if we follow this guide http://celery.readthedocs.org/en/latest/userguide/tasks.html#automatic-naming-and-relative-imports , it fails with ImportError
. This is because old django project structure. We should
如果我们遵循这个指南,http://celery.readthedocs.org/en/latest/userguide/tasks.html#automatic-naming- And -relative-import,它就会在ImportError中失败。这是因为旧的django项目结构。我们应该
- change
app = Celery('myproj', backend='redis://localhost', broker='redis://localhost:6379/0')
toapp = Celery('myproj', backend='redis://localhost', broker='redis://localhost:6379/0', include=['myproj.app1.tasks'])
- change app =芹菜('myproj',后端='redis://localhost', broker='redis://localhost:6379/0')到app =芹菜('myproj',后端='redis://localhost', broker='redis://localhost:6379/0',包括=[' myprojects .app1.tasks'])。
- comment out
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
- 注释掉app.autodiscover_tasks(λ:settings.INSTALLED_APPS)
- add project directory to celery
sys.path.append(os.path.abspath(os.pardir))
- 添加项目目录到芹菜sys.path.append(os.path.abspath(os.pardir))
All these problems come from different task names from server and client side. We should make sure they are the same
所有这些问题都来自服务器和客户端不同的任务名称。我们应该确保它们是一样的。
#1
0
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
will register tasks in app1 directory as app1.tasks.add
, tasks sent from shell will generate task with the same name. But tasks from web generates task named as myproj.app1.tasks.add
. So it complains KeyError
.
autodiscover_tasks(lambda: settings.INSTALLED_APPS)将在app1目录中作为app1任务注册任务。此外,来自shell的任务将以相同的名称生成任务。但是,来自web的任务会生成名为myprojects .app1.task .add的任务。所以KeyError抱怨道。
Changing from tasks import add
to from app1.tasks import add
solves the problem.
从任务导入添加到app1。任务导入添加解决了问题。
And if we follow this guide http://celery.readthedocs.org/en/latest/userguide/tasks.html#automatic-naming-and-relative-imports , it fails with ImportError
. This is because old django project structure. We should
如果我们遵循这个指南,http://celery.readthedocs.org/en/latest/userguide/tasks.html#automatic-naming- And -relative-import,它就会在ImportError中失败。这是因为旧的django项目结构。我们应该
- change
app = Celery('myproj', backend='redis://localhost', broker='redis://localhost:6379/0')
toapp = Celery('myproj', backend='redis://localhost', broker='redis://localhost:6379/0', include=['myproj.app1.tasks'])
- change app =芹菜('myproj',后端='redis://localhost', broker='redis://localhost:6379/0')到app =芹菜('myproj',后端='redis://localhost', broker='redis://localhost:6379/0',包括=[' myprojects .app1.tasks'])。
- comment out
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
- 注释掉app.autodiscover_tasks(λ:settings.INSTALLED_APPS)
- add project directory to celery
sys.path.append(os.path.abspath(os.pardir))
- 添加项目目录到芹菜sys.path.append(os.path.abspath(os.pardir))
All these problems come from different task names from server and client side. We should make sure they are the same
所有这些问题都来自服务器和客户端不同的任务名称。我们应该确保它们是一样的。