Django和Celery:未导入的任务

时间:2022-02-02 20:29:24

I am using Django with Celery to run two tasks in the background related to contacts/email parsing.

我正在使用Django和Celery在后台运行与联系人/电子邮件解析相关的两个任务。

Structure is:

project
   /api
   /core
       tasks.py
   settings.py

settings.py file contains:

settings.py文件包含:

BROKER_URL = 'django://'
BROKER_BACKEND = "djkombu.transport.DatabaseTransport"

#celery
BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"

sys.path.append(os.path.dirname(os.path.basename(__file__)))
CELERY_IMPORTS = ['project.core.tasks']

import djcelery
djcelery.setup_loader()

# ....

INSTALLED_APPS = (
    #...

    'kombu.transport.django',
    'djcelery',
)

tasks.py contains:

from celery.task import Task
from celery.registry import tasks

class ParseEmails(Task):
    #...

class ImportGMailContactsFromGoogleAccount(Task):
    #...

tasks.register(ParseEmails)
tasks.register(ImportGMailContactsFromGoogleAccount)

Also, added in wsgi.py

另外,在wsgi.py中添加

os.environ["CELERY_LOADER"] = "django"

Now, I have this app hosted on a WebFactional server. On my localhost this runs fine, but on the WebFaction server, where the Django app is deployed on a Apache server, I get:

现在,我将此应用程序托管在WebFactional服务器上。在我的localhost上运行正常,但在WebFaction服务器上,Django应用程序部署在Apache服务器上,我得到:

2013-01-23 17:25:00,067: ERROR/MainProcess] Task project.core.tasks.ImportGMailContactsFromGoogleAccount[df84e03f-9d22-44ed-a305-24c20407f87c] raised exception: Task of kind 'project.core.tasks.ImportGMailContactsFromGoogleAccount' is not registered, please make sure it's imported.

But the tasks show up as registered. If I run

但任务显示为已注册。如果我跑

python2.7 manage.py celeryd -l info

I obtain:

 -------------- celery@web303.webfaction.com v3.0.13 (Chiastic Slide)
---- **** ----- 
--- * ***  * -- [Configuration]
-- * - **** --- . broker:      django://localhost//
- ** ---------- . app:         default:0x1e55350 (djcelery.loaders.DjangoLoader)
- ** ---------- . concurrency: 8 (processes)
- ** ---------- . events:      OFF (enable -E to monitor this worker)
- ** ---------- 
- *** --- * --- [Queues]
-- ******* ---- . celery:      exchange:celery(direct) binding:celery
--- ***** ----- 

[Tasks]
  . project.core.tasks.ImportGMailContactsFromGoogleAccount
  . project.core.tasks.ParseEmails

I thought it could be a relative import error, but I assumed the changes in settings.py and wsgi.py would prevent that.

我认为这可能是一个相对导入错误,但我认为settings.py和wsgi.py中的更改会阻止这种情况。

I am thinking the multiple Python version supported by WebFactional could have to do with this, however I installed all the libraries for Python 2.7 and I am also running Django for 2.7, so there should be no problem with that.

我认为WebFactional支持的多个Python版本可能与此有关,但我安装了Python 2.7的所有库,我也运行Django for 2.7,所以应该没有问题。

Running in localhost using celeryd -l info the Tasks also show up in the list when I start the worker but it doesn't output the error when I call the task - it runs perfectly.

使用celeryd -l info在localhost中运行当我启动worker时,Tasks也会显示在列表中但是当我调用任务时它不会输出错误 - 它运行完美。

Thank you

1 个解决方案

#1


2  

I had the same issue in a new Ubuntu 12.04 / Apache / mod_wsgi / Django 1.5 / Celery 3.0.13 production environment. Everything works fine on my Mac Os X 10.8 laptop and my old server (which has Celery 3.0.12), but not on the new server.

我在新的Ubuntu 12.04 / Apache / mod_wsgi / Django 1.5 / Celery 3.0.13生产环境中遇到了同样的问题。我的Mac OS X 10.8笔记本电脑和我的旧服务器(具有Celery 3.0.12)上的一切正常,但新服务器上没有。

It seems there is some issue in Celery: https://github.com/celery/celery/issues/1150

似乎Celery存在一些问题:https://github.com/celery/celery/issues/1150

My initial solution was changing my Task class based task to @task decorator based, from something like this:

我的初始解决方案是将基于任务类的任务更改为基于@task的装饰器,如下所示:

class CreateInstancesTask(Task):
    def run(self, pk):
        management.call_command('create_instances', verbosity=0, pk=pk)
tasks.register(CreateInstancesTask)

to something like this:

这样的事情:

@task()
def create_instances_task(pk):
    management.call_command('create_instances', verbosity=0, pk=pk)

Now this task seems to work, but of course I've to do some further testing...

现在这项任务似乎有效,但我当然要做进一步测试......

#1


2  

I had the same issue in a new Ubuntu 12.04 / Apache / mod_wsgi / Django 1.5 / Celery 3.0.13 production environment. Everything works fine on my Mac Os X 10.8 laptop and my old server (which has Celery 3.0.12), but not on the new server.

我在新的Ubuntu 12.04 / Apache / mod_wsgi / Django 1.5 / Celery 3.0.13生产环境中遇到了同样的问题。我的Mac OS X 10.8笔记本电脑和我的旧服务器(具有Celery 3.0.12)上的一切正常,但新服务器上没有。

It seems there is some issue in Celery: https://github.com/celery/celery/issues/1150

似乎Celery存在一些问题:https://github.com/celery/celery/issues/1150

My initial solution was changing my Task class based task to @task decorator based, from something like this:

我的初始解决方案是将基于任务类的任务更改为基于@task的装饰器,如下所示:

class CreateInstancesTask(Task):
    def run(self, pk):
        management.call_command('create_instances', verbosity=0, pk=pk)
tasks.register(CreateInstancesTask)

to something like this:

这样的事情:

@task()
def create_instances_task(pk):
    management.call_command('create_instances', verbosity=0, pk=pk)

Now this task seems to work, but of course I've to do some further testing...

现在这项任务似乎有效,但我当然要做进一步测试......