简体   繁体   English

Python Django RabbitMQ芹菜导入任务问题

[英]Python django rabbitmq celery problems with importing tasks

Welcome... I'm creating a project where I parse xlsx files with xlrd library. 欢迎...我正在创建一个项目,在其中使用xlrd库解析xlsx文件。 Everything works just fine. 一切正常。 Then I configured RabbitMQ and Celery. 然后,我配置了RabbitMQ和Celery。 Created some tasks in main folder which works and can be accessed from iPython. 在主文件夹中创建了一些可以正常工作的任务,可以从iPython对其进行访问。 The problems starts when I'm in my application (application created back in time in my project) and I try to import tasks from my app in my views.py I tried to import it with all possible paths but everytime it throws me an error. 当我进入应用程序(在我的项目中及时创建的应用程序)并且尝试从我的应用程序中的views.py中导入任务时,问题就开始了,我尝试使用所有可能的路径导入它,但是每次它抛出一个错误时。 Official documentation posts the right way of importing tasks from other applications, It looks like this: from project.myapp.tasks import mytask But it doesn't work at all. 官方文档发布了从其他应用程序导入任务的正确方法,如下所示: from project.myapp.tasks import mytask但这根本不起作用。 In addition when Im in iPython I can import tasks with command from tango.tasks import add And it works perfectly. 另外,当我在iPython中使用Im时,我可以from tango.tasks import add导入命令from tango.tasks import add而且效果很好。

Just bellow I'm uploading my files and error printed out by console. 只是下面我正在上传我的文件,控制台输出了错误。

views.py views.py

# these are the instances that I was trying to import that seemed to be the most reasonable, but non of it worked
# import tasks
# from new_tango_project.tango.tasks import add
# from new_tango_project.tango import tasks
# from new_tango_project.new_tango_project.tango.tasks import add
# from new_tango_project.new_tango_project.tango import tasks
# from tango import tasks

#function to parse files
def parse_file(request, file_id):
    xlrd_file = get_object_or_404(xlrdFile, pk = file_id)
    if xlrd_file.status == False
        #this is some basic task that I want to enter to
        tasks.add.delay(321,123)

settings.py settings.py

#I've just posted things directly connected to celery
import djcelery
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tango',
    'djcelery',
    'celery',
)

BROKER_URL = "amqp://sebrabbit:seb@localhost:5672/myvhost"
BROKER_HOST = "127.0.0.1"
BROKER_PORT = 5672
BROKER_VHOST = "myvhost"
BROKER_USER = "sebrabbit"
BROKER_PASSWORD = "seb"
CELERY_RESULT_BACKEND = 'amqp://'
CELERY_TASK_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT=['json']
CELERY_TIMEZONE = 'Europe/Warsaw'
CELERY_ENABLE_UTC = False

celery.py (in my main folder new_tango_project ) celery.py(在我的主文件夹new_tango_project

from __future__ import absolute_import
import os
from celery import Celery
import djcelery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new_tango_project.settings')

app = Celery('new_tango_project')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# CELERY_IMPORTS = ['tango.tasks']
# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
    CELERY_RESULT_BACKEND='djcelery.backends.cache:CacheBackend',
)

if __name__ == '__main__':
    app.start()

tasks.py (in my main project folder new_tango_project ) task.py(在我的主项目文件夹new_tango_project

from __future__ import absolute_import
from celery import Celery
from celery.task import task

app = Celery('new_tango_project',
             broker='amqp://sebrabbit:seb@localhost:5672/myvhost',
             backend='amqp://',
             include=['tasks'])

@task
def add(x, y):
    return x + y


@task
def mul(x, y):
    return x * y


@task
def xsum(numbers):
    return sum(numbers)

@task
def parse(file_id, xlrd_file):

    return "HAHAHAHHHAHHA"

tasks.py in my application folder 我的应用程序文件夹中的task.py

from __future__ import absolute_import
from celery import Celery
from celery.task import task    
#
app = Celery('tango')

@task
def add(x, y):
    return x + y

@task
def asdasdasd(x, y):
    return x + y

celery console when starting 芹菜控制台启动时

 -------------- celery@debian v3.1.17 (Cipater)
---- **** ----- 
--- * ***  * -- Linux-3.2.0-4-amd64-x86_64-with-debian-7.8
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         new_tango_project:0x1b746d0
- ** ---------- .> transport:   amqp://sebrabbit:**@localhost:5672/myvhost
- ** ---------- .> results:     amqp://
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- 
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery

Finally my console log... 最后我的控制台日志...

[2015-02-20 11:19:45,678: ERROR/MainProcess] Received unregistered task of type 'new_tango_project.tasks.add'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'utc': True, 'chord': None, 'args': (123123123, 123213213), 'retries': 0, 'expires': None, 'task': 'new_tango_project.tasks.add', 'callbacks': None, 'errbacks': None, 'timelimit': (None, None), 'taskset': None, 'kwargs': {}, 'eta': None, 'id': 'd9a8e560-1cd0-491d-a132-10345a04f391'} (233b)
Traceback (most recent call last):
  File "/home/seb/PycharmProjects/tango/local/lib/python2.7/site-packages/celery/worker/consumer.py", line 455, in on_task_received
    strategies[name](message, body,
KeyError: 'new_tango_project.tasks.add'

This is the log from one of many tries importing the tasks. 这是来自导入任务的许多尝试之一的日志。

Where I`m making mistake ? 我在哪里犯错?

Best wishes 最好的祝愿

Hint 1: In all your tasks.py you declare your Celery app as app = Celery(...) but you don't specify which app the task should be attached to in your task decorators. 提示1:在所有tasks.py您将Celery应用app = Celery(...)声明为app = Celery(...)但未在任务装饰器中指定任务应附加到哪个应用程序。

Try to change your @task into @app.task and see if it works. 尝试将您的@task更改为@app.task ,看看是否@app.task

Hint 2: Why do you need to create a new Celery app in every tasks.py ? 提示2:为什么需要在每个tasks.py创建一个新的Celery应用程序? Why don't you just import one main Celery app with from new_tango_project.celery import app and then declare your tasks with @app.task ? 为什么不from new_tango_project.celery import app导入一个主要的Celery应用from new_tango_project.celery import app ,然后使用@app.task声明任务?

Hint 3: Once you have your tasks defined (possibly both in celery.py and tasks.py in the applications), just do 提示3:一旦定义了任务(可能同时在celery.py和应用程序中的tasks.py中),就执行

from new_tango_project.celery import add
from my_app.tasks import add_bis

def my_view(request):
    ...
    add.delay(*your_params)   # using the task from your celery.py
    add_bis.delay(*your_params) # your task from the application

I wonder how you start up your celery worker. 我想知道您如何启动您的芹菜工作者。 I encounter this once because I didn't start worker right: You should add -A option when execute "celery worker -l info" so that celery will connect to the broker you configured in your Celery Obj. 我曾经遇到过一次,因为我没有正确启动worker:执行“ celery worker -l info”时应添加-A选项,以便celery将连接到您在Celery Obj中配置的代理。 Otherwise celery will try to connect the default broker. 否则,celery将尝试连接默认代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM