module documentation

Functions that can be called inside of activities.

Most of these functions use contextvars to obtain the current activity in context. This is already set before the start of the activity. Activities that make calls that do not automatically propagate the context, such as calls in another thread, should not use the calls herein unless the context is explicitly propagated.

Class Info Information about the running activity.
Class LoggerAdapter Adapter that adds details to the log about the running activity.
Function defn Decorator for activity functions.
Function heartbeat Send a heartbeat for the current activity.
Function in_activity Whether the current code is inside an activity.
Function info Current activity's info.
Function is_cancelled Whether a cancellation was ever requested on this activity.
Function is_worker_shutdown Whether shutdown has been invoked on the worker.
Function metric_meter Get the metric meter for the current activity.
Function payload_converter Get the payload converter for the current activity.
Function raise_complete_async Raise an error that says the activity will be completed asynchronously.
Function shield_thread_cancel_exception Context manager for synchronous multithreaded activities to delay cancellation exceptions.
Async Function wait_for_cancelled Asynchronously wait for this activity to get a cancellation request.
Function wait_for_cancelled_sync Synchronously block while waiting for a cancellation request on this activity.
Async Function wait_for_worker_shutdown Asynchronously wait for shutdown to be called on the worker.
Function wait_for_worker_shutdown_sync Synchronously block while waiting for shutdown to be called on the worker.
Variable logger Logger that will have contextual activity details embedded.
Class _CompositeEvent Undocumented
Class _Context Undocumented
Class _Definition Undocumented
Exception _CompleteAsyncError Undocumented
Variable _current_context Undocumented
@overload
def defn(fn: CallableType) -> CallableType:
@overload
def defn(*, name: Optional[str] = None, no_thread_cancel_exception: bool = False) -> Callable[[CallableType], CallableType]:
@overload
def defn(*, no_thread_cancel_exception: bool = False, dynamic: bool = False) -> Callable[[CallableType], CallableType]:
(source)

Decorator for activity functions.

Activities can be async or non-async.

Parameters
fn:Optional[CallableType]The function to decorate.
name:Optional[str]Name to use for the activity. Defaults to function __name__. This cannot be set if dynamic is set.
no_thread_cancel_exception:boolIf set to true, an exception will not be raised in synchronous, threaded activities upon cancellation.
dynamic:boolIf true, this activity will be dynamic. Dynamic activities have to accept a single 'Sequence[RawValue]' parameter. This cannot be set to true if name is present.
def heartbeat(*details: Any): (source)

Send a heartbeat for the current activity.

Raises
RuntimeErrorWhen not in an activity.
def in_activity() -> bool: (source)

Whether the current code is inside an activity.

Returns
boolTrue if in an activity, False otherwise.
def info() -> Info: (source)

Current activity's info.

Returns
InfoInfo for the currently running activity.
Raises
RuntimeErrorWhen not in an activity.
def is_cancelled() -> bool: (source)

Whether a cancellation was ever requested on this activity.

Returns
boolTrue if the activity has had a cancellation request, False otherwise.
Raises
RuntimeErrorWhen not in an activity.
def is_worker_shutdown() -> bool: (source)

Whether shutdown has been invoked on the worker.

Returns
boolTrue if shutdown has been called on the worker, False otherwise.
Raises
RuntimeErrorWhen not in an activity.

Get the metric meter for the current activity.

Warning

This is only available in async or synchronous threaded activities. An error is raised on non-thread-based sync activities when trying to access this.

Returns
temporalio.common.MetricMeterCurrent metric meter for this activity for recording metrics.
Raises
RuntimeErrorWhen not in an activity or in a non-thread-based synchronous activity.

Get the payload converter for the current activity.

This is often used for dynamic activities to convert payloads.

def raise_complete_async() -> NoReturn: (source)

Raise an error that says the activity will be completed asynchronously.

@contextmanager
def shield_thread_cancel_exception() -> Iterator[None]: (source)

Context manager for synchronous multithreaded activities to delay cancellation exceptions.

By default, synchronous multithreaded activities have an exception thrown inside when cancellation occurs. Code within a "with" block of this context manager will delay that throwing until the end. Even if the block returns a value or throws its own exception, if a cancellation exception is pending, it is thrown instead. Therefore users are encouraged to not throw out of this block and can surround this with a try/except if they wish to catch a cancellation.

This properly supports nested calls and will only throw after the last one.

This just runs the blocks with no extra effects for async activities or synchronous multiprocess/other activities.

Raises
temporalio.exceptions.CancelledErrorIf a cancellation occurs anytime during this block and this is not nested in another shield block.
async def wait_for_cancelled(): (source)

Asynchronously wait for this activity to get a cancellation request.

Raises
RuntimeErrorWhen not in an async activity.
def wait_for_cancelled_sync(timeout: Optional[Union[timedelta, float]] = None): (source)

Synchronously block while waiting for a cancellation request on this activity.

This is essentially a wrapper around threading.Event.wait.

Parameters
timeout:Optional[Union[timedelta, float]]Max amount of time to wait for cancellation.
Raises
RuntimeErrorWhen not in an activity.
async def wait_for_worker_shutdown(): (source)

Asynchronously wait for shutdown to be called on the worker.

Raises
RuntimeErrorWhen not in an async activity.
def wait_for_worker_shutdown_sync(timeout: Optional[Union[timedelta, float]] = None): (source)

Synchronously block while waiting for shutdown to be called on the worker.

This is essentially a wrapper around threading.Event.wait.

Parameters
timeout:Optional[Union[timedelta, float]]Max amount of time to wait for shutdown to be called on the worker.
Raises
RuntimeErrorWhen not in an activity.

Logger that will have contextual activity details embedded.

Undocumented