3

Im going over this list here but i cant get it working :

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

So what i did was: 1) downloaded celery modules and added them to my manage.py and wsgi.py:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os, sys
from ConfigParser import RawConfigParser

config = RawConfigParser()
abspath = os.path.abspath(os.path.dirname(__file__))

config.read(abspath+'/subfolder/settings.ini')

homedir = config.get('paths', 'libspath')
projectspath = config.get('paths', 'projectspath')

path = [
    '/Django-1.4.5/', 
    '/South-0.7.6',
    '/python-openid-master',#07.05.2013 checkout from https://github.com/openid/python-openid
    '/mbi-django-rosetta-eca151e',
    '/phonenumbers-5.2b1',
    '/django-phonenumber-field-develop',
    '/django-openid-auth-0.5',
    '/celery-3.0.19',
    '/django-celery-3.0.17',
    '/kombu-2.5.11',
    '/billiard-2.7.3.28',
    '/anyjson-0.3.3',
    '/amqp-1.0.12'
    ]

for item in path:
    module = homedir+item
    if module not in sys.path:
        sys.path.append(module)

2) installed rabbitmq 3) set up stuff in settings.py:

INSTALLED_APPS = (
    ---
    'djcelery',
)

and in the end:

BROKER_URL = 'amqp://guest:guest@localhost:5672/'
import djcelery
djcelery.setup_loader()

4) ran python manage.py syncdb

5) since im using mod_wsgi i also added this to my wsgi.py (in addition to code i showed earlier)

import djcelery
djcelery.setup_loader()

6) i created tasks.py in my core app:

import logging, subprocess
logger = logging.getLogger('debugger')

from django.conf import settings

from celery import task

@task
def runfunc(funcname, refno):
    x = 'nothing to see here'

7) i run: python manage.py celery worker --loglevel=info and get whole bunch of stuff meaning that its all good :P.

But when i go the django view which is supposed to run the task i get:

cannot import name task
Request Method: GET
Request URL:    http://localhost/url/that/triggers/task/
Django Version: 1.4.5
Exception Type: ImportError
Exception Value:    
cannot import name task
Exception Location: /path/to/project/core/tasks.py in <module>, line 6

When i go to manage.py shell and type from celery import task - it works just fine. if i import the function from core.tasks, then i get exactly same error message.

Can anyone explain to me wth is up here..

Alan

4

2 回答 2

5

尝试将 tasks.py 更改为:

from __future__ import absolute_import
from celery import task

您可能有一个celery/orcelery.py与抛出错误 ( ) 的模块位于同一目录中tasks.py,这作为相对导入失败。

于 2013-07-01T21:20:31.167 回答
1

哎呀,你在第一步中从哪里得到所有这些?我从来不需要更改manage.pyorwsgi.py文件来运行其他包。你说“下载的芹菜模块”然后你就有了所有的sys.path修改代码。这让我相信你没有使用pipand virtualenv。使用这两个工具,您的生活将变得更加轻松。你会简化很多事情,你的团队会永远爱你。当 Python 包管理存在以消除这种痛苦时,不要过于复杂。很少需要路径修改。

你提到的那些Celery 第一步真的是它所需要的。看起来你已经处理了那个特别的mod_wsgi笔记。我的猜测是问题出在第一步——无论你对 manage.py 和 wsgi.py 做了什么修改。所有其他步骤看起来都很好*。

我会复制你的代码,创建一个新的 virtualenv,pip 安装这些包并删除所有路径设置的东西。用于django-admin.py startproject xxx查看普通香草manage.pywsgi.py文件的外观。也可DEBUG=True用于帮助进行故障排除。当这可行并且您解决了问题时,将您的替换settings.ini为 pip需求文件

* 你使用 South 所以记住python manage.py migrate djcelery。(在您的第 4 步中,您只使用syncdb)。我不相信这样做会修复导入错误,但在更大的情况下这是必要的。

于 2013-07-01T23:12:15.340 回答