Skip to navigation

Experimental. For evaluation. Interfaces, configuration, and behavior may change incompatibly.

Provides workflow control flow and event-driven concurrency.

Prerequisites

  • None.

Setup

Provides common patterns for flow control and data types. Each pattern defines a mapping from a control case to a handler for that case. Patterns may be nested and, will cihild patterns will be provided with details about the case that was selected by the parent. Cases may be functions, which receive a precursor object detailing any previous selections. The returned value is used for selection purposes. Each handler is a function that receives a single value, the precursor, that provides information about the case that was chosen. The return value for the handler is pattern specific. See each pattern’s documentation for additional details.

Members

halt

patterns.haltpatterns.Halt

A value compelling patterns to halt iterating through cases.

deadline

patterns.deadline(expiry)

Create a deadline event source that fires once, at a fixed duration from now. Unlike patterns.timeout(), the deadline is not reset when other branches fire in a patterns.select() loop — the timer starts when patterns.deadline() is called and is shared across all iterations of a patterns.sequence().

Parameters
expirystringTime until the deadline expires (e.g. ”30s”, “5m”, “1h”).
Returns
event_sourcepatterns.DeadlineEventSourceAn event source that fires once, at the absolute deadline.

pool

patterns.pool(scenario, items, fn, concurrency?)

Map an iterable through a function with bounded concurrency. Returns an iterable that yields results in completion order.

Parameters
scenariostringA title for the pool scenario, used for identification in logs.
itemsiterableAn iterable of items to process.
fncallableA callable that takes an item and returns an EventSourceProvider (or any value for pass-through).
concurrency?optionalintMax concurrent operations. 0 or omitted = unbounded.
Returns
poolanyAn iterable that yields results in completion order.

select

patterns.select(scenario, cases, id?, until?)

Use an event driven architecture to invoke handlers based on workflow signals. A dict provides mapping from events to event handlers. Each key is an event source, provided by other modules, to represent events within their lifecycles. The handler may return any value, which will be returned by the select() call. When until is set, select loops until any handler returns that value.

Parameters
scenariostringA title for the scenario. This is used to disambiguate and identify scenarios in a script.
casesiterablemappingMapping from events to a handler for the event.
id?optionalstringAn identifier for the pattern. May be used by downstream consumers to differentiate scenarios.
until?optionalanyLoop until any handler returns this value. Defaults to None (single-shot).
Returns
valueanyThe final value.

semaphore

patterns.semaphore(capacity)

Create a bounded concurrency semaphore. Use with patterns.wait() or patterns.select() to acquire a token, and call release() to return it.

Parameters
capacityintThe maximum number of concurrent tokens.
Returns
semaphorepatterns.SemaphoreA semaphore that can be used as an event source.

sequence

patterns.sequence(scenario, cases, id?, start?, strict?)

Use a state-machine like architecture to drive behavior. Each step provides the next step in the sequence or a value to return. The key values of the provided dict specifies the set of steps in the sequence. Each dict value defines a handler that may do some work, but returns the next step to execute. If the returned value is not in the dict, then the value is returned from the sequence() call. patterns.sequence(“state machine example”, { “start”: “state1”, “state1”: lambda p: state1_handler(p), “state2”: “state1”, “state3”: patterns.halt, }, start=“start”, strict=True)

Parameters
scenariostringA title for the scenario. This is used to disambiguate and identify scenarios in a script.
casesiterablemappingA mapping of states that make up the sequence. Each value is either a value that is a key in the map, a function that will compute a key or value that will be returned.
id?optionalstringAn identifier for the pattern. May be used by downstream consumers to differentiate scenarios.
start?optionalanyThe starting step. None by default.
strict?optionalboolIf true, fail for unknown values.
Returns
valueanyThe final value.

spawn

patterns.spawn(fn)

Launch a function asynchronously in a Temporal coroutine. Returns an event source that fires when the function completes. Use with patterns.select() or patterns.wait() to collect the result.

Parameters
fnanyA callable (typically a lambda) to execute asynchronously. The function may call activities, read from Jira, etc.
Returns
event_sourcepatterns.SpawnEventSourceAn event source that fires when the function completes. p.value in handlers is the function’s return value.

switch

patterns.switch(scenario, operand, cases, id?, otherwise?)

Utilize a switch-like pattern where multiple conditions are evaluated. An operand value is provided and compared against the keys of the provided dict. The first match with trigger the corresponding handler. Because both the operand and the keys may be functions, complex logic can be presented in a compact form. patterns.switch(“example”, True, { False: “this won’t be matched”, lambda p: True: “this will be matched”, })

Parameters
scenariostringA title for the scenario. This is used to disambiguate and identify scenarios in a script.
operandanyA value or function that provides the value to choose from.
casesiterablemappingA mapping of states that make up the sequence. Each value is either a value that is a key in the map, a function that will compute a key or value that will be returned.
id?optionalstringAn identifier for the pattern. May be used by downstream consumers to differentiate scenarios.
otherwise?optionalanyThe starting step.
Returns
valueanyThe final value.

timeout

patterns.timeout(duration)

Create a timeout event source that fires after the given duration. Use with patterns.select() or patterns.wait().

Parameters
durationstringThe duration to wait (e.g. ”30s”, “5m”, “24h”).
Returns
event_sourcepatterns.TimeoutEventSourceAn event source that fires when the timeout expires.

wait

patterns.wait(scenario, event_source, timeout?)

Wait for a single event source to fire, with an optional timeout. Returns the resolved value on success, None on timeout. Sugar over patterns.select().

Parameters
scenariostringA title for the wait scenario, used for identification in logs and traces.
event_sourceanyAn event source to wait on (e.g. from patterns.spawn(), interact.form(), wi.case_jql()).
timeout?optionalstringOptional timeout duration (e.g. ”30s”, “5m”). Returns None if the timeout fires before the event source.
Returns
valueanyThe resolved value from the event source, or None on timeout.