简体   繁体   English

芹菜没有与烧瓶应用一起运行

[英]Celery not running with flask application

I'm using celery in my flask application but celery(3.1.8).This is my configuration with the flask application 我在烧瓶应用中使用芹菜,但芹菜(3.1.8)。这是我的烧瓶应用配置

celery.py celery.py

 from __future__ import absolute_import
 from celery import Celery
 from cuewords.settings import CELERY_BROKER_URL,CELERY_RESULT_BACKEND

 app = Celery('proj',
         broker=CELERY_BROKER_URL,
         backend=CELERY_RESULT_BACKEND)

app.conf.update(CELERY_TASK_RESULT_EXPIRES=3600)

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

setting.py setting.py

  CELERY_BROKER_URL='redis://localhost:6379/0'
  CELERY_RESULT_BACKEND='redis://localhost:6379/0'
  BROKER_TRANSPORT = 'redis'

api.py api.py

 class Webcontent(Resource):
   def post(self,session=session):
   args = self.parser.parse_args()
   site_url = args["url"]
   url_present=Websitecontent.site_url_present(session,site_url)
    if site_url.strip() != "" and not url_present:
      try:
        #add data and commit 
        session.commit()
        websitecontent=Websitecontent(params*)
        websitecontent.update_url(id,session)
      except:
        session.rollback()
      raise
      finally:
        session.close()                
    else:
      return "No data created / data already present"

And in my model i'm adding a method to task 在我的模型中,我正在添加一个方法来完成任务

model.py model.py

  from cuewords.celery import app
  class Websitecontent(Base):

    @app.task(name='update_url')
    def update_url(self,id,session):
    ...code goes here..

And this how i run the celery from command prompt 这是我如何从命令提示符运行芹菜

celery -A cuewords.celery worker

And i also using flower to monitor the task i can see a worker running but i couldn't see any task its empty .Any idea what im missing or doing wrong .. Thanks 而且我还使用花来监控任务,我可以看到一个工人正在运行,但我看不到任何任务它是空的。任何想法我失踪或做错了..谢谢

The problem is that your tasks never get imported into the Python runtime when running the worker(s). 问题是,在运行worker时,您的任务永远不会导入Python运行时。 The celery command is your entry point. celery命令是您的切入点。 And you're telling Celery to import your cuewords.celery module because thats where you're app instance resides. 而且你告诉Celery导入你的cuewords.celery模块,因为那是你应用实例所在的地方。 However, this is where the chain of events ends and no further Python code is imported. 但是,这是事件链结束的地方,不再导入其他Python代码。

Now, the most common mistake is to import the tasks into the same module as the Celery app instance. 现在,最常见的错误是将任务导入与Celery应用程序实例相同的模块中。 Unfortunately this will result in two modules trying to import things from each other and will result in a circular import error. 不幸的是,这将导致两个模块尝试从彼此导入内容并导致循环导入错误。 This is no good. 这不好。

To get around this one could import the task functions into the Celery app module and register them without using the decorator style. 为了解决这个问题,可以将任务函数导入到Celery应用程序模块中,并在不使用装饰器样式的情况下注册它们。 For example: 例如:

from celery import Celery
from models import my_task

app = Celery()
app.task(name='my_task')(my_task)

This would remove the need to import the app instance in your model module. 这将消除在模型模块中导入应用程序实例的需要。

However, you're using method tasks. 但是,您正在使用方法任务。 Method tasks need to be treated differently than function tasks as noted here: http://docs.celeryproject.org/en/latest/reference/celery.contrib.methods.html . 方法任务需要与功能任务区别对待,如下所示: http//docs.celeryproject.org/en/latest/reference/celery.contrib.methods.html Method tasks are different from function tasks, because they are associated with an instance of an object. 方法任务与功能任务不同,因为它们与对象的实例相关联。 In other words, the function is a class function. 换句话说,该函数是一个类函数。 So to use the previous style of registering tasks, you'd need an instance of the class first. 因此,要使用先前的注册任务样式,首先需要该类的实例。 To get around this you should consider making your tasks functions instead of methods. 要解决这个问题,您应该考虑使用您的任务而不是方法。

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

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