GitHub

durable_deferred

API reference for durable_deferred.

Named wait points of a workflow execution (Effect's effect/workflow/DurableDeferred)::

approval = durable_deferred.make("approval", success=S.Boolean)

# in the workflow: suspend until someone completes it
approved = yield from approval.wait()

# anywhere else, with the token the workflow handed out
yield from approval.succeed(token=token, value=True)

A deferred's result is stored by the engine, encoded by its schemas, so a replay of the workflow reads it instead of waiting. A token names the workflow, the execution and the deferred, so code outside the workflow can complete the right wait point.

Values

AttributeTypeDescription
Tokentype[Token]

A string naming a workflow, an execution and a deferred.

TokenSchemaBrandSchema[Token, str, Never, Never]

Tokens (Effect's DurableDeferred.Token).

TOKEN_FROM_STRINGDecodeToSchema[TokenParsed, str, Never, Never]

Tokens as strings, decoded to TokenParsed (Effect's TokenParsed.FromString).

Classes

Functions

make

make[A = None, E = Never](name, *, success=None, error=None) -> DurableDeferred[A, E]

A deferred named name (unique within its workflow). success defaults to None (Effect's Void), error to none.

ParameterTypeDescription
namestr
successCodec[A, Any, Never, Never] | None = None
errorCodec[E, Any, Never, Never] | None = None

Returns DurableDeferred[A, E]

def make[A = None, E = Never](
    name: str,
    *,
    success: Codec[A, Any, Never, Never] | None = None,
    error: Codec[E, Any, Never, Never] | None = None,
) -> DurableDeferred[A, E]:
    """A deferred named ``name`` (unique within its workflow). ``success``
    defaults to ``None`` (Effect's ``Void``), ``error`` to none."""
    return DurableDeferred(
        name,
        success=cast("Codec[A, Any, Never, Never]", NullSchema) if success is None else success,
        error=cast("Codec[E, Any, Never, Never]", NeverSchema) if error is None else error,
    )

await_

await_[A, E](self) -> Effect[A, E, WorkflowEngine | WorkflowInstance]

self's result, suspending the execution until there is one.

ParameterTypeDescription
selfDurableDeferred[A, E]

Returns Effect[A, E, WorkflowEngine | WorkflowInstance]

def await_[A, E](self: DurableDeferred[A, E]) -> Effect[A, E, WorkflowEngine | WorkflowInstance]:
    """``self``'s result, suspending the execution until there is one."""

    def body() -> Generator[Effect[Any, Any, Any], Any, A]:
        engine = yield from WorkflowEngine()
        instance = yield from WorkflowInstance()
        # Register before the read, so any later completion can preempt the run.
        _register_await(instance, self.name)
        found = yield from wrap_activity_result(engine.deferred_result(self), lambda e: e is None)
        if found is None:
            return (yield from suspend_run(instance))
        return (yield from from_exit(found))

    return gen(body)

race_all

race_all[A, E, R](*, name, success, error, effects) -> Effect[A, E, R | WorkflowEngine | WorkflowInstance]

Race effects durably: the first to succeed wins (or the last failure), its exit is recorded, and a replay gets the recorded exit without running them again.

ParameterTypeDescription
namestr
successExactly[A]
errorExactly[E]
effectsSequence[Effect[A, E, R]]

Returns Effect[A, E, R | WorkflowEngine | WorkflowInstance]

def race_all[A, E, R](
    *,
    name: str,
    success: Exactly[A],
    error: Exactly[E],
    effects: Sequence[Effect[A, E, R]],
) -> Effect[A, E, R | WorkflowEngine | WorkflowInstance]:
    """Race ``effects`` durably: the first to succeed wins (or the last
    failure), its exit is recorded, and a replay gets the recorded exit
    without running them again."""
    if not effects:
        raise ValueError("race_all needs at least one effect")
    deferred: DurableDeferred[A, E] = DurableDeferred(
        f"raceAll/{name}", success=codec_of(success), error=codec_of(error)
    )
    return _record_held(race_all_effects(*effects), deferred, check_cache=True)