简体   繁体   English

如何在失败时恢复 Prefect 流程而无需重新运行整个流程?

[英]How to resume a Prefect flow on failure without having to re-run the entire flow?

TL;DR; TL;博士;

I wasn't able to use prefect's FlowRunner to solve the above question.我无法使用 prefect 的FlowRunner来解决上述问题。 I likely either used it wrong (see below) or missed something.我可能要么用错了(见下文),要么错过了一些东西。 Would really appreciate any pointers!真的很感激任何指点!


The Problem问题

I read through the fantastic prefect core documentation and found the sections on Handling Failure and Local Debugging to be the most relevant to this (may have missed something.).我通读了出色的核心文档,发现 处理失败本地调试的部分与此最相关(可能遗漏了一些东西。)。 The FlowRunner class appeared (to me) to be the solution. FlowRunner class 似乎(对我来说)是解决方案。

To see if I could use Flow Runner to resume a failed flow:看看我是否可以使用 Flow Runner 来恢复失败的流程:

  • Made a failing flow run:进行了失败的流程运行:
from time import sleep

import prefect
from prefect import Flow, task


@task
def success():
    sleep(3)
    return


@task
def failure():
    return 1 / 0


def get_flow_runner():
    with Flow("Success/Failure") as flow:

        success()
        failure()

    return prefect.engine.FlowRunner(flow)
  • Ran it in iPython and saved the state:在 iPython 中运行它并保存 state:
In [1]: run nameofscript.py
In [2]: flow_runner = get_flow_runner()
In [3]: state = flow_runner.run()
  • Replaced 1 / 0 with 1 / 1 in failure() so task would be successful:failure()中将 1 / 0 替换为 1 / 1 以便任务成功:

  • And finally passed the previous state to the flow_runner hoping that it would resume the flow:最后将之前的 state 传递给flow_runner ,希望它能够恢复流程:

In [1]: run nameofscript.py
In [2]: flow_runner = get_flow_runner()
In [3]: flow_runner.run(task_states=state.result)

The entire flow ran again including the 3 second successful task.整个流程再次运行,包括 3 秒成功的任务。

The issue here is that you are rebuilding your Flow with each run, which changes the Task objects.这里的问题是您在每次运行时都在重建 Flow,这会更改 Task 对象。 state.result is a dictionary whose keys are Task objects - if the underlying Task object changes in any way, so will its hash. state.result是一个字典,其键是任务对象 - 如果基础任务 object 发生任何变化,那么它的 hash 也会发生变化。 You should instead manually create the dictionary of states using the updated Task objects, like so:您应该改为使用更新的 Task 对象手动创建状态字典,如下所示:

from prefect.engine.state import Success

failure_task = runner.flow.get_tasks(name="failure")[0]
task_states = {failure_task: Success("Mocked success")}

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

相关问题 如何根据从 Prefect 2.0 的 Prefect 流程中传递的变量动态重命名任务运行? - How to dynamically rename a task run based on passed variable from within a Prefect flow for Prefect 2.0? 如何在 Prefect 服务器上远程部署流程? - How to deploy flow remotly on Prefect server? 如何使用 cmd arguments 组织完美流程? - How to organize prefect flow with the using of cmd arguments? 如何在完美中为 control_flow 分配名称? - How to assign a name to a control_flow in prefect? 如何使用 Prefect 使用不同的参数多次运行流程? - How can I run a flow several times using different parameters using Prefect? ModuleNotFoundError 有一个 Prefect 流程,但不是另一个 - ModuleNotFoundError with one Prefect flow, but not the other 如何在具有完美的流中缓存/定位具有相同名称的任务? - How to cache/target tasks with the same name in a Flow with prefect? 为什么完美流程会因深度递归错误而崩溃? - Why prefect flow crashes with deep recursion error? PyCharm - 我如何调试(如在 Jupyter Notebook 中)而不必每次修改代码时都重新运行完整的脚本? - PyCharm - how do I debug (like in Jupyter Notebook) without having to re-run the full script every-time that I modify code? 使用 with 和 object Flow() 为 python 创建完美流时出错 - Error to create a flow in prefect for python using with and object Flow()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM