Jayendra Gothi
Jayendra Gothi

Reputation: 712

Inside celery Task Class run method get task_id

I am trying to run following code:

class myTask(Task):
    def run():
        print myTask.request.id

But this code is giving None as request_id. Please Explain me why i am not able to read id in side celery Task Class

Upvotes: 1

Views: 912

Answers (1)

daniula
daniula

Reputation: 7028

You are trying to access request object on class not object instance. Try this:

class myTask(Task):
    def run(self, *args, **kwargs):
        print self.request.id

You can also use @task decorator:

app = Celery('tasks', broker='amqp://guest@localhost//')

@app.task(bind=True)
def myTask(self):
    print self.request.id

Upvotes: 1

Related Questions