prefect.transactions

Functions

get_transaction

get_transaction() -> BaseTransaction | None

transaction

transaction(key: str | None = None, store: ResultStore | None = None, commit_mode: CommitMode | None = None, isolation_level: IsolationLevel | None = None, overwrite: bool = False, write_on_commit: bool = True, logger: logging.Logger | LoggingAdapter | None = None) -> Generator[Transaction, None, None]
A context manager for opening and managing a transaction. Args:
  • -: An identifier to use for the transaction
  • -: The store to use for persisting the transaction result. If not provided, a default store will be used based on the current run context.
  • -: The commit mode controlling when the transaction and child transactions are committed
  • -: Whether to overwrite an existing transaction record in the store
  • -: Whether to write the result to the store on commit. If not provided, will default will be determined by the current run context. If no run context is available, the value of PREFECT_RESULTS_PERSIST_BY_DEFAULT will be used.

atransaction

atransaction(key: str | None = None, store: ResultStore | None = None, commit_mode: CommitMode | None = None, isolation_level: IsolationLevel | None = None, overwrite: bool = False, write_on_commit: bool = True, logger: logging.Logger | LoggingAdapter | None = None) -> AsyncGenerator[AsyncTransaction, None]
An asynchronous context manager for opening and managing an asynchronous transaction. Args:
  • -: An identifier to use for the transaction
  • -: The store to use for persisting the transaction result. If not provided, a default store will be used based on the current run context.
  • -: The commit mode controlling when the transaction and child transactions are committed
  • -: Whether to overwrite an existing transaction record in the store
  • -: Whether to write the result to the store on commit. If not provided, the default will be determined by the current run context. If no run context is available, the value of PREFECT_RESULTS_PERSIST_BY_DEFAULT will be used.

Classes

IsolationLevel

Methods:

auto

auto() -> str
Exposes enum.auto() to avoid requiring a second import to use AutoEnum

CommitMode

Methods:

auto

auto() -> str
Exposes enum.auto() to avoid requiring a second import to use AutoEnum

TransactionState

Methods:

auto

auto() -> str
Exposes enum.auto() to avoid requiring a second import to use AutoEnum

BaseTransaction

A base model for transaction state. Methods:

add_child

add_child(self, transaction: Self) -> None

get

get(self, name: str, default: Any = NotSet) -> Any
Get a stored value from the transaction. Child transactions will return values from their parents unless a value with the same name is set in the child transaction. Direct changes to returned values will not update the stored value. To update the stored value, use the set method. Args:
  • name: The name of the value to get
  • default: The default value to return if the value is not found
Returns:
  • The value from the transaction
Examples: Get a value from the transaction:
with transaction() as txn:
    txn.set("key", "value")
    ...
    assert txn.get("key") == "value"
Get a value from a parent transaction:
with transaction() as parent:
    parent.set("key", "parent_value")
    with transaction() as child:
        assert child.get("key") == "parent_value"
Update a stored value:
with transaction() as txn:
    txn.set("key", [1, 2, 3])
    value = txn.get("key")
    value.append(4)
    # Stored value is not updated until `.set` is called
    assert value == [1, 2, 3, 4]
    assert txn.get("key") == [1, 2, 3]

    txn.set("key", value)
    assert txn.get("key") == [1, 2, 3, 4]

get

get(cls: type[Self]) -> Optional[Self]
Get the current context instance

get_active

get_active(cls: Type[Self]) -> Optional[Self]

get_parent

get_parent(self) -> Self | None

is_active

is_active(self) -> bool

is_committed

is_committed(self) -> bool

is_pending

is_pending(self) -> bool

is_rolled_back

is_rolled_back(self) -> bool

is_staged

is_staged(self) -> bool

model_copy

model_copy(self: Self) -> Self
Duplicate the context model, optionally choosing which fields to include, exclude, or change. Returns:
  • A new model instance.

prepare_transaction

prepare_transaction(self) -> None
Helper method to prepare transaction state and validate configuration.

serialize

serialize(self, include_secrets: bool = True) -> dict[str, Any]
Serialize the context model to a dictionary that can be pickled with cloudpickle.

set

set(self, name: str, value: Any) -> None
Set a stored value in the transaction. Args:
  • name: The name of the value to set
  • value: The value to set
Examples: Set a value for use later in the transaction:
with transaction() as txn:
    txn.set("key", "value")
    ...
    assert txn.get("key") == "value"

stage

stage(self, value: Any, on_rollback_hooks: Optional[list[Callable[..., Any]]] = None, on_commit_hooks: Optional[list[Callable[..., Any]]] = None) -> None
Stage a value to be committed later.

Transaction

A model representing the state of a transaction. Methods:

add_child

add_child(self, transaction: Self) -> None

begin

begin(self) -> None

commit

commit(self) -> bool

get

get(self, name: str, default: Any = NotSet) -> Any
Get a stored value from the transaction. Child transactions will return values from their parents unless a value with the same name is set in the child transaction. Direct changes to returned values will not update the stored value. To update the stored value, use the set method. Args:
  • name: The name of the value to get
  • default: The default value to return if the value is not found
Returns:
  • The value from the transaction
Examples: Get a value from the transaction:
with transaction() as txn:
    txn.set("key", "value")
    ...
    assert txn.get("key") == "value"
Get a value from a parent transaction:
with transaction() as parent:
    parent.set("key", "parent_value")
    with transaction() as child:
        assert child.get("key") == "parent_value"
Update a stored value:
with transaction() as txn:
    txn.set("key", [1, 2, 3])
    value = txn.get("key")
    value.append(4)
    # Stored value is not updated until `.set` is called
    assert value == [1, 2, 3, 4]
    assert txn.get("key") == [1, 2, 3]

    txn.set("key", value)
    assert txn.get("key") == [1, 2, 3, 4]

get_active

get_active(cls: Type[Self]) -> Optional[Self]

get_parent

get_parent(self) -> Self | None

is_active

is_active(self) -> bool

is_committed

is_committed(self) -> bool

is_pending

is_pending(self) -> bool

is_rolled_back

is_rolled_back(self) -> bool

is_staged

is_staged(self) -> bool

prepare_transaction

prepare_transaction(self) -> None
Helper method to prepare transaction state and validate configuration.

read

read(self) -> ResultRecord[Any] | None

reset

reset(self) -> None

rollback

rollback(self) -> bool

run_hook

run_hook(self, hook: Callable[..., Any], hook_type: str) -> None

set

set(self, name: str, value: Any) -> None
Set a stored value in the transaction. Args:
  • name: The name of the value to set
  • value: The value to set
Examples: Set a value for use later in the transaction:
with transaction() as txn:
    txn.set("key", "value")
    ...
    assert txn.get("key") == "value"

stage

stage(self, value: Any, on_rollback_hooks: Optional[list[Callable[..., Any]]] = None, on_commit_hooks: Optional[list[Callable[..., Any]]] = None) -> None
Stage a value to be committed later.

AsyncTransaction

A model representing the state of an asynchronous transaction. Methods:

add_child

add_child(self, transaction: Self) -> None

begin

begin(self) -> None

commit

commit(self) -> bool

get

get(self, name: str, default: Any = NotSet) -> Any
Get a stored value from the transaction. Child transactions will return values from their parents unless a value with the same name is set in the child transaction. Direct changes to returned values will not update the stored value. To update the stored value, use the set method. Args:
  • name: The name of the value to get
  • default: The default value to return if the value is not found
Returns:
  • The value from the transaction
Examples: Get a value from the transaction:
with transaction() as txn:
    txn.set("key", "value")
    ...
    assert txn.get("key") == "value"
Get a value from a parent transaction:
with transaction() as parent:
    parent.set("key", "parent_value")
    with transaction() as child:
        assert child.get("key") == "parent_value"
Update a stored value:
with transaction() as txn:
    txn.set("key", [1, 2, 3])
    value = txn.get("key")
    value.append(4)
    # Stored value is not updated until `.set` is called
    assert value == [1, 2, 3, 4]
    assert txn.get("key") == [1, 2, 3]

    txn.set("key", value)
    assert txn.get("key") == [1, 2, 3, 4]

get_active

get_active(cls: Type[Self]) -> Optional[Self]

get_parent

get_parent(self) -> Self | None

is_active

is_active(self) -> bool

is_committed

is_committed(self) -> bool

is_pending

is_pending(self) -> bool

is_rolled_back

is_rolled_back(self) -> bool

is_staged

is_staged(self) -> bool

prepare_transaction

prepare_transaction(self) -> None
Helper method to prepare transaction state and validate configuration.

read

read(self) -> ResultRecord[Any] | None

reset

reset(self) -> None

rollback

rollback(self) -> bool

run_hook

run_hook(self, hook: Callable[..., Any], hook_type: str) -> None

set

set(self, name: str, value: Any) -> None
Set a stored value in the transaction. Args:
  • name: The name of the value to set
  • value: The value to set
Examples: Set a value for use later in the transaction:
with transaction() as txn:
    txn.set("key", "value")
    ...
    assert txn.get("key") == "value"

stage

stage(self, value: Any, on_rollback_hooks: Optional[list[Callable[..., Any]]] = None, on_commit_hooks: Optional[list[Callable[..., Any]]] = None) -> None
Stage a value to be committed later.