Getting task_id inside a Celery task

后端 未结 3 374
夕颜
夕颜 2021-01-04 05:48

This is probably a stupid question but its got me stumped coming from a Ruby background.

I have an object that looks like this when I try to print it.



        
3条回答
  •  误落风尘
    2021-01-04 06:22

    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.

提交回复
热议问题