Temporal.io Python SDK

1.24.0 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart demonstrates how to define an activity and a workflow, configure and start a Temporal worker to process tasks, and execute a workflow. It connects to a local Temporal server (defaulting to `localhost:7233`) and includes `os.environ.get` for flexible configuration.

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())

view raw JSON →