Getting task_id inside a Celery task

In order to make your tasks more "OO-like", you could use the bind argument to get a reference to self:

@celery.task(bind=True)
def scan(self, host):
  print self.request.id

Please note that self.request.id is actually an instance of AsyncTask. In order to have the task id as a string, you should do self.request.id.__str__().

From Celery's documentation (after the example):

The bind argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance.


Short story, within function scan, use scan.request.id.

See http://docs.celeryproject.org/en/latest/userguide/tasks.html?highlight=request#task-request-info