[英]How to use Xcom within python script file executed by BashOperator
I am executing a python script file using a Bash Operator as following:我正在使用 Bash 运算符执行 python 脚本文件,如下所示:
op = BashOperator(task_id='python',
bash_command='python3 py_script.py')
In the python file, I want to send a value to xcom if a condition is met in a loop, something like:在 python 文件中,如果在循环中满足条件,我想向 xcom 发送一个值,例如:
for i in range(10):
if i == 2:
print(i)
xcom_push('hello')
Since it is in a loop and I want it to continue after i reach 2, I can't use a return
.由于它处于循环中,并且我希望它在达到 2 后继续,所以我不能使用
return
。 I have tried with:我尝试过:
from airflow.models import XCom
context['ti'].xcom_push('hello')
kwargs['ti'].xcom_push('hello')
But in both cases I receive an error like NameError: name 'kwargs' is not defined
但在这两种情况下,我都会收到类似
NameError: name 'kwargs' is not defined
的错误
What would be the solution?解决方案是什么? Thanks
谢谢
If you set xcom_push=True
in BashOperator
then the last line written to stdout by the bash command will be pushed to an XCom ( docs ).如果您在
BashOperator
中设置xcom_push=True
,则 bash 命令写入标准输出的最后一行将被推送到 XCom( 文档)。
So, if after print(i)
in your py_script.py
no other lines are printed then the string representation of i
will be pushed to an XCom for the pythonk
task.因此,如果在您的
py_script.py
中的print(i)
之后没有打印其他行,则i
的字符串表示将被推送到 XCom 以执行pythonk
任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.