Hey guys, A question about sub-flows concurrency basic control issue: I have the need to create a ...
s

Srul Pinkas

over 1 year ago
Hey guys, A question about sub-flows concurrency basic control issue: I have the need to create a flow to run sub-flow that are a bit heavy (on memory mostly, take ~10 min to run), and i need it with a few dozens different-inputs. i'm using prefect 2.20 I tried two approaches: - simply trigger each sub-flow (through
run_deployment
in order to allocate separate resources). this creates dozens sub-flows but the parent-job trying to keep updated and manage it crashes due to connection-limit reached error (an internal
httpx
crash). I asked marvin about configuring it on prefect and didn't see any immediate solution, perhaps more than 4-5 running in parallel is too much for prefect? am i missing something? - create many sub-flows but run it through a managed concurrency-limit queue (say, 3 at each given time - once one is done another kicks in). since it's a flow and not a task i had to go through work-pools /work-queue limits configuration. the thing is that since it takes some time to get the actual machine (mostly for the first batch from cluster), during that time it seems there's an error for the sub-flow yet to be executed (-1 signal until later it succeeds). and due to that temp-failure, other sub-flows also start to run and i'm missing my 3-limit (so 6-7 are running in parallel) which is odd as it seems like a common glitch. I looked for a parameter that controls how much time to wait for the first sub-flow-submit failure to avoid this race-edge-case but the only parameter (
job_watch_timeout_seconds
) that the docs were promising about, was also described as: "Number of seconds to wait for each event emitted by a job before timing out" - so raising this to 1-2 minutes seems too important/risky. - The third fallback option i use is creating less but chubbier jobs - each running serially 10 different inputs - and running them in parallel. but that seems like a bad solution for such a platform that should take care of that.. Did i miss any simple way out of this? Thanks!
Nesting multiple asynchronous flows in Prefect can be managed using the concept of subflows or neste...
m

Marvin

over 1 year ago
Nesting multiple asynchronous flows in Prefect can be managed using the concept of subflows or nested flows. Here's how it works, based on both Prefect 2.x and 3.x: ### Prefect 2.x In Prefect 2.x, you can create subflows by calling a flow function inside another flow. The parent flow is the main flow, while the child or "subflow" is executed within it. Subflows behave like regular flow runs and have their own task runners. Importantly, subflows block the parent flow's execution until completion, but with asynchronous subflows, you can run them concurrently using
asyncio.gather
. For more on this, you can check the Prefect 2.x documentation on nested flows. ### Prefect 3.x In Prefect 3.x, nested flows offer several benefits, such as using different task runners and enabling conditional execution of grouped tasks. They provide first-class observability, meaning you can track the status of nested flows easily within the Prefect UI. Nested flows also support asynchronous execution, allowing them to be awaited or run concurrently, adhering to async Python standards. For a deeper dive into creating asynchronous nested flows in Prefect 3.x, review the Prefect 3.x documentation on writing flows. In essence, whether you're using Prefect 2.x or 3.x, the key to nesting asynchronous flows lies in leveraging subflows with async capabilities and managing their execution so they can handle concurrent workloads effectively. Ah, the joys of managing asynchronous tasks—always making you feel like a juggler who needs just one more hand!