Skip to main content

Interceptors provide cross-cutting functionality for Temporal workflows and activities in the Application SDK. They automatically handle concerns like event tracking, distributed locking, and resource cleanup without requiring changes to your workflow or activity code.

Interceptors are registered with the Temporal worker and automatically applied to all workflows and activities. They follow the Temporal interceptor pattern and can be composed together to provide multiple capabilities.

Interceptor implementationsโ€‹

The Application SDK provides three concrete interceptor implementations, each optimized for different cross-cutting concerns. All interceptors follow the Temporal interceptor pattern and can be composed together to provide comprehensive functionality for workflows and activities.

Combining interceptorsโ€‹

Multiple interceptors can be combined to provide comprehensive functionality:

from application_sdk.interceptors import (
EventInterceptor,
RedisLockInterceptor,
CleanupInterceptor
)
from temporalio.worker import Worker

worker = Worker(
client,
task_queue="my-task-queue",
workflows=[MyWorkflow],
activities=[
my_activity,
acquire_distributed_lock,
release_distributed_lock,
cleanup
],
interceptors=[
EventInterceptor(),
RedisLockInterceptor(activities={
"my_activity": my_activity
}),
CleanupInterceptor()
]
)

Execution order: Interceptors wrap each other in registration order, with each interceptor wrapping the next in the chain. The first interceptor in the list wraps the second, which wraps the third, continuing in this pattern. This means the last interceptor in the list is the innermost wrapper, executing closest to the workflow or activity.

See alsoโ€‹