Skip to main content

Advanced: Long-lived Parent

View as Markdown

Keep a fixed pool of worker Steps running while the Flow accepts more requests.

The Init Step publishes the initial requests to RequestChannel and starts N HandleRequest Step executions. Each worker takes the oldest message and moves to the HandleSubFlow Step. That Step starts ExampleSubFlow and waits for it. When the child completes, the Step loops back to the HandleRequest Step. If the Stop RPC has set Stopped to true, it completes gracefully instead. Stopped has no value until the Stop RPC sets it, and only that RPC writes it. A Stop that arrives before Init finishes is therefore kept.

SendRequest checks the Channel size before it publishes. When the Channel reaches the buffer threshold, it returns false. The caller can then back off and retry, and the parent is not overloaded.

Core implementation

advanced_long_live_parent_flow.pyPython
DEFAULT_CONCURRENCY = 10
MAX_BUFFERED_REQUESTS = 100


class LongLiveInit(Step[ParentInput]):
def __init__(self, request_channel: Channel[str]) -> None:
self.request_channel = request_channel

def get_step_type(self) -> str:
return "Init"

def execute(self, context: Context, input: ParentInput) -> StepDecision:
for request in input.requests:
self.request_channel.publish(context, request)
concurrency = input.concurrency if input.concurrency > 0 else DEFAULT_CONCURRENCY
return go_to_many(
*(StepMovement.of(LongLiveHandleRequest, None) for _ in range(concurrency))
)


class LongLiveHandleRequest(Step[None]):
def __init__(self, request_channel: Channel[str]) -> None:
self.request_channel = request_channel

def get_step_type(self) -> str:
return "HandleRequest"

def wait_for(self, context: Context, input: None) -> Wait:
return Wait.until(self.request_channel.for_one())

Example: examples/python/dex_examples/patterns/parallel-subflows/advanced_long_live_parent_flow.py

Close the intake

The runnable examples above show the basic variant: a later Step starts each child, and a Stop RPC ends the workers after their children finish. They have no closable intake, shared admission lock, or returned-request reply. A parent that closes its intake for good also ends its idle workers and hands back its queued requests:

  • A close RPC marks the intake closed and publishes a close message. From then on, the admission RPC rejects new requests, so callers can resubmit to a new parent run.
  • Both RPCs and the worker's Execute in HandleRequest declare the same intakeClosed lock.
  • Each worker owns the request it consumed. Under that lock it only commits its next move: to HandleChild, which holds no lock and starts the child, or, after the close, to ReturnRequest, which holds no lock and sends the returned request back through an idempotent path keyed by the request, such as a reply to its submitter, then takes the next request. The RPCs take the lock, so no worker calls another system or Flow while holding it. A queued request is never dropped or handed back twice.
  • A close Step waits for the close message and completes gracefully.
  • Channel conditions still win over the graceful completion condition, so after the close, workers keep taking and returning queued requests. Once the queue is empty, idle workers wake through the graceful completion condition and end their branch. Workers with a running child finish it first.
  • The admission RPC checks the Channel size for its limit. Every RPC receives Channel sizes, so the parent keeps no separate waiting counter.
  • The graceful completion condition needs SDK and server support; see Graceful completion requests. Without it, the close Step publishes one wake message per worker, and each worker also waits for that wake Channel, declared after the request Channel.

These steps describe the closable-intake variant. Its extra Steps, such as CloseIntake, are not in the graph above.

  1. sendRequest holds the intakeClosed lock. It accepts a request while the intake is open and the current queue length is below the limit. It queues the request in the Requests Channel.
  2. Each worker in HandleRequest waits for a queued request or for a graceful completion request.
  3. A worker that took a request holds the intakeClosed lock and commits its move to HandleChild, which starts the child without the lock. After the close, it moves to ReturnRequest instead, which sends the returned request without the lock and then waits for the next one.
  4. A worker whose wait ended through the graceful completion request, with no request consumed, ends its branch.
  5. closeIntake holds the intakeClosed lock, marks the intake closed, and publishes a close message. CloseIntake receives it and completes the Flow gracefully.

Next