简体   繁体   中英

Access Arguments to Parent Task from Subtask in Celery

Is it possible to access the arguments with which a parent task A was called, from its child task Z? Put differently, when Task Z gets called in a chain, can it somehow access an argument V that was invoked when Task A was fired, but that was not passed through any intermediary nodes between tasks A and Z ? And if so, how?

Using Celery 3.0 with RabbitMQ for results backend.

I'm going to provisionally answer my own question with a "no" -- and the hopes that someone more knowledgeable than me will come back with a solution.

After looking more closely at the Celery documentation, I'm thinking that there simply isn't a way to access the arguments with which a parent task was called. According to the documentation :

During its lifetime a task will transition through several possible states, and each state may have arbitrary metadata attached to it. When a task moves into a new state the previous state is forgotten about..

A task can be in one of the following states: Pending, Started, Success, Failure, Retry, and Revoked. For my scenario, the key here would, it seems, be the Success state. What I want to do is, for a given succeessful task, locate the id of (one of its) parent task(s), and then view the arguments with which the parent was called. However, according to the documentation, a task in the SUCCESS state will not contain this sort of data (since the parent task itself will have been successful insofar as it has spawned a child task):

SUCCESS Task has been successfully executed.

metadata: result contains the return value of the task. propagates: Yes ready: Yes

In other words, it would seem that there's a hard limit here. Even if I can locate the ID of the parent task I'm interested in, because it has successfully executed, I will by definition not be able to access the arguments with which it was called because it only contains the return value (no longer its arguments).

I think you can, at least in chord . When you bind=True your task, you can access self.request . In self.request.chord You can find a detailed dict. In its kwargs or options['chord'] you will find what you're looking for, but it's not an elegant solution. Also, if the parent has been replaced, you will only be able to see the final state.

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