Background
So now we want to first get a list from one operator, and then iterate the result and run another operator.
The script is as follows:
def hello_world(ti, execution_date, **context):
# Do sth here and generate the value final_output
ti.xcom_push(key='whatever', value=final_output)
dag = DAG(
"test",
schedule_interval=None,
start_date=datetime.datetime(2021, 5, 17),
catchup=False,
)
with dag:
t1 = PythonOperator(
task_id="hello_world",
python_callable=hello_world,
)
outcome_list = "{{ ti.xcom_pull(key='whatever',task_ids='hello_world') }}"
for x in outcome_list:
t2 = PythonOperator(
task_id="test_{x}",
python_callable=do_sth,
op_kwargs={"input_param": x},
)
t1 >> t2
The current situation is, we managed to get the xcom variable. The list is always around with 60 elements, which will not cause any performance issue. However, it is returned as a string of list.
To iterate it, we want to transform it to a list and pass to the function which runs the operator in t2
Current issue
The outcome_list is generated via jinja template and saved as a str like this
['user_A US', 'user_B BR' , ..... ]
We tried to convert the outcome_list into a proper python string with the following function in the DAG:
outcome_list = outcome_list.strip("[]").split(", ")
It returns error as follows
jinja2.exceptions.TemplateSyntaxError: unexpected end of template, expected ','.
And when we tried to convert the output into list with jinja syntax
outcome_list = "{{ ti.xcom_pull(key='whatever',task_ids='hello_world') | list }}"
We got error when performing the loop, said that it is not itertable.
Whats's the issue here and how should we process? Thank you for the help!!

