简体   繁体   English

如何在 BashOperator 执行的 python 脚本文件中使用 Xcom

[英]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.

相关问题 如何使用 python 运算符在气流 dag 文件中使用 xCom? - How to use xCom in airflow dag file using python operator? 使用 XCom Pull in Airflow Dag 从 Python Callable 调用 BashOperator - Calling a BashOperator from a Python Callable with XCom Pull in Airflow Dag 我用BashOperator在Airflow中执行了一个python的文件,如何导入其他自定义的function? - I use BashOperator to execute a python file in Airflow, how to import other self-defined function? Airflow 从 BashOperator 到 SSHOperator 的 XCOM 通信 - Airflow XCOM communication from BashOperator to SSHOperator 如何评估将在Jupyter Notebook中执行的python脚本的路径 - How to evaluate the path to a python script to be executed within Jupyter Notebook Airflow:Python 脚本如何在使用 BashOperator 运行时发出失败的任务信号 - Airflow: How can Python script signal a failed task when run using BashOperator Python : 想要使用在 python 中传递的命令行参数,作为将在 python 脚本中执行的另一个命令的参数 - Python : want to use the command line argument passed in python, as an argument to another command that will be executed within the python script python:如何判断文件是作为导入还是主脚本执行的? - python: how to tell if file executed as import vs. main script? 如何在 python 脚本中使用 shell 变量 - How to use shell variables within a python script 如何在 Airflow 运算符中应用 Python 函数超过 Airflow XCom_pull 值 - How to apply Python Functions over Airflow XCom_pull value within Airflow Operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM