Temporal.io Python SDK
The Temporal.io Python SDK (current version 1.24.0) provides a framework for authoring durable workflows and activities using the Python programming language. It enables developers to build resilient, scalable, and fault-tolerant applications that can execute long-running business logic reliably. The library maintains a regular release cadence, often with monthly or bi-monthly updates, introducing new features, improvements, and bug fixes.
Warnings
- breaking Python 3.9 support was removed in version 1.19.0 as it reached End-of-Life. Users on Python 3.9 must upgrade to Python 3.10 or newer.
- breaking In version 1.23.0, fields `workflow_id`, `workflow_namespace`, `workflow_run_id`, and `workflow_type` within `activity.Info` were made optional. Additionally, `converter.BaseWorkflowSerializationContext` was removed.
- breaking Version 1.24.0 introduces general availability for Nexus and OpenAI Agents SDK Integration. It also includes new OpenTelemetry integration for OpenAI Agents, which may cause conflicts or require adjustments for existing custom OpenTelemetry setups.
- gotcha Beginning with version 1.21.0, providing an `api_key` to `Client.connect` automatically enables TLS.
- gotcha The Workflow Sandbox isolates workflow code to ensure determinism. Importing non-standard library or non-`temporalio` modules directly into workflow files can lead to `RestrictedWorkflowAccessError` or non-determinism.
Install
-
pip install temporalio
Imports
- Client
from temporalio.client import Client
- Worker
from temporalio.worker import Worker
- workflow
from temporalio import workflow
- activity
from temporalio import activity
- workflow.unsafe.imports_passed_through
from temporalio import workflow with workflow.unsafe.imports_passed_through(): from my_app import my_activity_module
Quickstart
import asyncio
from datetime import timedelta
from temporalio.client import Client
from temporalio.worker import Worker
from temporalio import activity, workflow
# Define an activity
@activity.defn
async def say_hello(name: str) -> str:
return f"Hello, {name}!"
# Define a workflow
@workflow.defn
class GreetingWorkflow:
@workflow.run
async def run(self, name: str) -> str:
return await workflow.execute_activity(
say_hello,
name,
schedule_to_close_timeout=timedelta(seconds=5),
)
async def main():
# Connect to Temporal server (default to localhost:7233)
# Use os.environ.get('TEMPORAL_HOST_PORT', 'localhost:7233') for dynamic connection
client = await Client.connect(os.environ.get('TEMPORAL_HOST_PORT', 'localhost:7233'))
# Run a worker
task_queue_name = "my-task-queue"
worker = Worker(
client,
task_queue=task_queue_name,
workflows=[GreetingWorkflow],
activities=[say_hello],
)
# Start worker in background
worker_task = asyncio.create_task(worker.run())
print(f"Worker started on task queue '{task_queue_name}'...")
# Start a workflow execution
result = await client.execute_workflow(
GreetingWorkflow.run,
"Temporal",
id="greeting-workflow-id",
task_queue=task_queue_name,
)
print(f"Workflow result: {result}") # Expected: "Hello, Temporal!"
# Clean up worker
worker_task.cancel()
await worker_task
if __name__ == "__main__":
import os
asyncio.run(main())