简体   繁体   中英

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. 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. 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.

To achieve the same I tried naive example mentioned on celery site and created a sample class. I am creating task using :

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.

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. 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

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

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

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

o/p of tester.py

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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