Hello, I'm looking for a way to pause a task if a ...
# ask-community
d
Hello, I'm looking for a way to pause a task if a certain condition is met, and resume it after the user confirms. Here is a minimal example:
Copy code
from prefect import task, Flow, Parameter
from prefect.engine.signals import PAUSE

@task
def dummy_task(x):
    confirmed = ???
    if x > 100 and not confirmed:
        raise PAUSE("Are you sure ?")
    return x + 1

with Flow("dummy") as flow:
    x = Parameter('x', default=1)
    dummy_task(x)
Is there a clean way of getting previous states of a task, maybe from
prefect.context
, such that I know confirmation happened ? Or do I have to add some branching to handle this ?
s
If you add a state handler onto the flow itself you should be able to inspect the state of each task in the flow whenever the state changes. So you could have separate handlers to handle state from RUNNING->PAUSE, PAUSE->RUNNING or whatever makes the most sense for your application https://docs.prefect.io/core/concepts/notifications.html#state-handlers
👍 2
d
Hello Sam, thank you ! I would not have though about handling it at the flow level, but it makes sense. 👍
After more digging I found an even simpler solution, using the context:
Copy code
import prefect
from prefect import task, Flow, Parameter
from prefect.engine.signals import PAUSE


@task
def dummy_task(x):
    confirmed = prefect.context.get("resume")
    if x > 100 and not confirmed:
        raise PAUSE("Are you sure ?")
    return x + 1


with Flow("dummy") as flow:
    x = Parameter('x', default=1)
    dummy_task(x)

flow.run()
Taking inspiration from the
manual_only
trigger :)