Python 死刑执行顺序

Python 死刑执行顺序,python,airflow,Python,Airflow,我正在检查示例代码 有两个“操作”功能: def my_sleeping_function(random_base): """This is a function that will run within the DAG execution""" time.sleep(random_base) 以及: 对于每次运行my_sleeping_函数我们运行print_context 我不明白的是顺序。 这是图表和树。。执行顺序不同: 先发生什么?之后会发生什么?为什么? 根据这一

我正在检查示例代码

有两个“操作”功能:

def my_sleeping_function(random_base):
    """This is a function that will run within the DAG execution"""
    time.sleep(random_base)
以及:

对于每次运行
my_sleeping_函数
我们运行
print_context

我不明白的是顺序。 这是图表和树。。执行顺序不同:

先发生什么?之后会发生什么?为什么?

根据这一点,我假设:

for i in range(5):
    task = PythonOperator(
        task_id='sleep_for_' + str(i),
        python_callable=my_sleeping_function,
        op_kwargs={'random_base': float(i) / 10},
        dag=dag)

    task.set_upstream(run_this)

run_这个执行,然后任务执行,但是循环让我困惑。

这里的循环只是向您展示如何动态构建DAG。执行的顺序取决于您设置为“上游”或“下游”任务的内容

您链接到的示例也可以像下面的示例一样完成。但是,如果您想再添加10项任务,该怎么办?为了实现同样的效果,您必须进行大量的复制/粘贴编码,最好像链接示例中那样将其放入循环中:

def my_sleeping_function(random_base):
    """This is a function that will run within the DAG execution"""
    time.sleep(random_base)


def print_context(ds, **kwargs):
    pprint(kwargs)
    print(ds)
    return 'Whatever you return gets printed in the logs'


run_this = PythonOperator(
    task_id='print_the_context',
    provide_context=True,
    python_callable=print_context,
    dag=dag)

task_0 = PythonOperator(
    task_id='sleep_for_' + 0,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(0) / 10},
    dag=dag)

task_1 = PythonOperator(
    task_id='sleep_for_' + 1,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(1) / 10},
    dag=dag)

task_2 = PythonOperator(
    task_id='sleep_for_' + 2,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(2) / 10},
    dag=dag)


task_3 = PythonOperator(
    task_id='sleep_for_' + 3,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(3) / 10},
    dag=dag)

task_4 = PythonOperator(
    task_id='sleep_for_' + 4,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(4) / 10},
    dag=dag)


task_0.set_upstream(run_this)
task_1.set_upstream(run_this)
task_2.set_upstream(run_this)
task_3.set_upstream(run_this)
task_4.set_upstream(run_this)

我认为您的困惑是基于您期望图形视图和树视图是同一事物的两个独立的可视化。然而,它们被用来想象不同的事物

图形视图显示任务在工作流中运行的顺序。在您的情况下,
print\u将运行\u上下文,
sleep\u for_0
sleep\u for_1
sleep\u for_2
sleep\u for_3
sleep\u for_4
将并行运行(或至少在气流配置允许的情况下并行运行)

树状视图表示DAG的深度优先可视化(以及右侧方框中每个任务随时间的状态)。也就是说,树中的第一级节点是dag(叶节点)中的最终任务,dag将被视为成功完成。它为每个必须运行才能运行的从属任务分支

换句话说,两个视图中的执行顺序是相同的,只是从不同的方向对其进行了可视化。

@cwartz是正确的

要设置任务的顺序,可以使用任务的参数
priority\u weight

pool参数可与priority_weight结合使用,以定义队列中的优先级,以及在池中打开插槽时首先执行哪些任务。()

def my_sleeping_function(random_base):
    """This is a function that will run within the DAG execution"""
    time.sleep(random_base)


def print_context(ds, **kwargs):
    pprint(kwargs)
    print(ds)
    return 'Whatever you return gets printed in the logs'


run_this = PythonOperator(
    task_id='print_the_context',
    provide_context=True,
    python_callable=print_context,
    dag=dag)

task_0 = PythonOperator(
    task_id='sleep_for_' + 0,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(0) / 10},
    dag=dag)

task_1 = PythonOperator(
    task_id='sleep_for_' + 1,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(1) / 10},
    dag=dag)

task_2 = PythonOperator(
    task_id='sleep_for_' + 2,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(2) / 10},
    dag=dag)


task_3 = PythonOperator(
    task_id='sleep_for_' + 3,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(3) / 10},
    dag=dag)

task_4 = PythonOperator(
    task_id='sleep_for_' + 4,
    python_callable=my_sleeping_function,
    op_kwargs={'random_base': float(4) / 10},
    dag=dag)


task_0.set_upstream(run_this)
task_1.set_upstream(run_this)
task_2.set_upstream(run_this)
task_3.set_upstream(run_this)
task_4.set_upstream(run_this)