简体   繁体   English

Prefect:如何根据从参数派生的任务列表强制创建任务

[英]Prefect: How to imperatively create tasks based on a task list derived from a Parameter

I'm trying to define tasks imperatively based on a list.我正在尝试根据列表强制定义任务。 The challenge is that the list should be based on a Prefect parameter.挑战在于该列表应基于 Prefect 参数。

Below is the code I tried, but apparently it doesn't work as task_dependency_pairs is a task, not a list.下面是我试过的代码,但显然它不起作用,因为 task_dependency_pairs 是一个任务,而不是一个列表。

How do I make it work without breaking the dependency between the Parameter task, and the other dynamically generated tasks?如何在不破坏 Parameter 任务和其他动态生成的任务之间的依赖关系的情况下使其工作?

from prefect import task, Flow, Parameter, Task
import time

@task
def task_dependency_pairs(param):
    return [
    ('task 1', f'{param}A', ''),
    ('task 2', f'{param}B','task 1'),
    ('task 3', f'{param}C','task 1')]

class Task_class(Task):

    def run(self, **kwarg):   
        time.sleep(5)
        print(f"This task {kwarg['task_name']} does a lot of things with {kwarg.get('calc_value','')}.")

for task_name, dependency in task_dependency_pairs:
    globals()[task_name] = type(task_name, (Task_class,),{"__module__": __name__})

with Flow("my_process") as flow:

    param = Parameter("param", default="default_param")
    task_dependency_pairs_list = task_dependency_pairs(param)
    for task_name, calc_value, dependency in task_dependency_pairs_list:   # This won't work
        task_instance = globals()[task_name](name=task_name)
        flow.add_task(task_instance(task_name = task_name, calc_value = calc_value))

    for task_name, calc_value, dependency in task_dependency_pairs_list:  # This won't work
        if len(dependency) >0:
            flow.get_tasks(name=task_name)[0].set_upstream(flow.get_tasks(name=dependency)[0])

flow.visualize()

Trying to dynamically create tasks in a prefect Flow is best managed via the mapping functionality.尝试在完美流程中动态创建任务最好通过映射功能进行管理

However, the mapping simply will generate tasks from an iterable during the flow run.但是,映射只会在流程运行期间从可迭代对象生成任务。 It doesn't arbitrarily tweak dependencies of the generated tasks;它不会随意调整生成任务的依赖关系; they all share the dependencies as defined on the main mapped task.它们都共享在主mapped任务上定义的依赖项。

However, if you wanted to generate a flow at runtime (with programmatic dependencies), the only way I can think of would be to create a task that creates a flow and immediately runs it.但是,如果您想在运行时生成一个流(具有编程依赖项),我能想到的唯一方法是创建一个任务来创建一个流并立即运行它。

The way this might look for your flow is:这可能会寻找您的流程的方式是:

...

@task
def run_flow(inputs):
    with Flow("subflow") as sub_flow:
        for (name, calc_value, dependency) in inputs:
            inst = Task_class(name=name)(task_name=name, calc_value=calc_value)
            sub_flow.add_task(inst)
            if dependency:
                inst.set_upstream(sub_flow.get_tasks(name=dependency)[0])

    sub_flow.run()

with Flow("my_process") as flow:
    param = Parameter("param", default="default_param")
    task_dependency_pairs_list = task_dependency_pairs(param)
    run_flow(task_dependency_pairs_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM