简体   繁体   English

用户定义的 celery 任务类:在导入期间调用 init

[英]User defined celery task class : init is getting called during import

I am trying to use celery task as a class and looking at following behavior.我正在尝试将 celery 任务用作一个类并查看以下行为。 I guess that I missed something.我想我错过了什么。 Let me first tell you what I am trying to achieve :让我先告诉你我想要达到的目标:
1. Create a class with its init function which would be called only once by celery. 1. 创建一个类,它的 init 函数只会被 celery 调用一次。 This will setup required params for my class.这将为我的班级设置所需的参数。 I am gonna create a threadpool here.我要在这里创建一个线程池。
2. Create instance of this celery task object in producer and put jobs in it. 2. 在生产者中创建此 celery 任务对象的实例并将作业放入其中。

To achieve the same I tried naive example mentioned on celery site and created a sample class.为了达到同样的目的,我尝试了 celery 站点上提到的天真示例并创建了一个示例类。 I am creating task using :我正在使用以下方法创建任务:

celery -c 1 -A proj worker --loglevel=debug celery -c 1 -A proj worker --loglevel=debug

it seems to be working at first but then I observed that init of task is getting called at import in tester.py, I could stop this init in object usage by passing flag but init during import is a real concern here.它起初似乎在工作,但后来我观察到在 tester.py 中的导入时调用了任务的 init,我可以通过传递标志来停止对象使用中的这个 init,但导入期间的 init 在这里是一个真正的问题。

Can you please point me to correct usage of this example.你能指点我正确使用这个例子吗? I do not want init of task class to be called more than what I invoked using celery command.我不希望任务类的 init 被调用的次数多于我使用 celery 命令调用的次数。 In real life scenario it would create unnecessary threads.在现实生活中,它会创建不必要的线程。

Also if possible, point me to right an example which is closest to my requirement mentioned above.另外,如果可能的话,请指出一个最接近我上面提到的要求的例子。

celery.py芹菜.py

from __future__ import absolute_import
from celery import Celery
app = Celery('proj',
         broker='amqp://',
         backend='amqp://',
         include=['proj.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
)

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

tasks.py任务.py

from __future__ import absolute_import
from proj.celery import app

class NaiveAuthenticateServer(app.Task):

def __init__(self, celeryInst = 1):
    if celeryInst == 1:
        print "Hi, I am celery instant"
    else :
        print "Did you invoke me from command"
    self.users = {'george': 'password'}

def run(self, username, password):
    try:
        return self.users[username] == password
    except KeyError:
        return False

tester.py测试器.py

from proj import tasks
obj = tasks.NaiveAuthenticateServer(0)
res = obj.delay('hi', 'hello')
print res.get()

o/p of tester.py tester.py 的 o/p

Hi, I am celery instant嗨,我是芹菜速溶
Did you invoke me from command你有没有从命令中调用我
False错误的

You should not create an instance of the task class yourself, but rather let celery do that for you automatically when the process starts up.您不应自己创建任务类的实例,而应让 celery 在进程启动时自动为您创建。

Therefore, you need to define a task function that uses the base class:因此,您需要定义一个使用基类的任务函数:

@app.task(base=NaiveAuthenticateServer)
def my_task(arg1, arg2):
    print arg1, arg2

And then submit the task like this:然后像这样提交任务:

from proj import tasks
tasks.my_task.delay('hi', 'hello')

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

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