{"id":7195,"library":"durabletask-azuremanaged","title":"Durable Task Azure Managed Provider","description":"`durabletask-azuremanaged` is a Python library that provides an implementation for the `durabletask` SDK, enabling Python applications to leverage Azure's Durable Task Scheduler. It allows developers to define, run, and manage long-running, stateful workflows and orchestrations on Azure by integrating with its managed infrastructure for reliable task execution. As of version 1.4.0, it aligns with the `durabletask-py` SDK, typically seeing new releases in conjunction with the broader SDK's development.","status":"active","version":"1.4.0","language":"en","source_language":"en","source_url":"https://github.com/Azure/durabletask-py/tree/main/durabletask-azuremanaged","tags":["azure","durable-task","orchestration","workflow","serverless","cloud"],"install":[{"cmd":"pip install durabletask-azuremanaged","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This library is a provider for the core Durable Task Python SDK.","package":"durabletask","optional":false}],"imports":[{"symbol":"AzureManagedTaskHub","correct":"from durabletask_azuremanaged.azure_managed_task_hub import AzureManagedTaskHub"}],"quickstart":{"code":"import asyncio\nimport os\nfrom durabletask.client import TaskHubClient\nfrom durabletask.orchestration import OrchestrationContext, orchestrator\nfrom durabletask.worker import Worker\nfrom durabletask_azuremanaged.azure_managed_task_hub import AzureManagedTaskHub\n\nasync def run_orchestration_sample():\n    # Configure Azure Managed Task Hub with connection string and task hub name\n    # AZURE_DURABLETASK_CONNECTION_STRING: Primary/Secondary connection string\n    #     from the Azure Durable Task Hub resource.\n    # AZURE_DURABLETASK_HUB_NAME: A globally unique name for your task hub within the region.\n    connection_string = os.environ.get(\"AZURE_DURABLETASK_CONNECTION_STRING\", \"\")\n    task_hub_name = os.environ.get(\"AZURE_DURABLETASK_HUB_NAME\", \"MyPythonTaskHub\")\n\n    if not connection_string:\n        print(\"Please set the AZURE_DURABLETASK_CONNECTION_STRING environment variable.\")\n        return\n\n    # 1. Initialize the Azure Managed Task Hub backend\n    task_hub = AzureManagedTaskHub(\n        connection_string=connection_string,\n        task_hub_name=task_hub_name\n    )\n\n    # 2. Define an orchestrator function\n    @orchestrator\n    async def my_orchestrator(context: OrchestrationContext, input_value: str):\n        print(f\"Orchestration '{context.instance_id}' started with input: {input_value}\")\n        result = await context.call_activity(\"my_activity\", input_value)\n        print(f\"Activity returned: {result}\")\n        return f\"Orchestration completed with result: {result}\"\n\n    # 3. Define an activity function\n    async def my_activity(context: OrchestrationContext, value: str):\n        print(f\"Activity '{context.instance_id}' received: {value}\")\n        await asyncio.sleep(1) # Simulate some work\n        return f\"Processed: {value.upper()}\"\n\n    # 4. Initialize the Worker and register orchestrator/activity\n    worker = Worker(task_hub)\n    worker.add_orchestrator(my_orchestrator)\n    worker.add_activity(\"my_activity\", my_activity)\n\n    # 5. Initialize the TaskHubClient for scheduling orchestrations\n    client = TaskHubClient(task_hub)\n\n    # 6. Start the worker in the background (essential for processing tasks)\n    worker_task = asyncio.create_task(worker.run())\n\n    try:\n        # 7. Schedule a new orchestration\n        print(\"Scheduling new orchestration...\")\n        instance_id = await client.schedule_new_orchestration(my_orchestrator, \"Hello DurableTask!\")\n        print(f\"Orchestration instance ID: {instance_id}\")\n\n        # 8. Wait for the orchestration to complete\n        status = await client.wait_for_completion(instance_id, timeout=30)\n        print(f\"\\nOrchestration '{instance_id}' completed with status: {status.runtime_status}\")\n        print(f\"Output: {status.output}\")\n\n    finally:\n        # 9. Clean up: Shut down the worker\n        print(\"\\nShutting down worker...\")\n        worker_task.cancel()\n        try:\n            await worker_task\n        except asyncio.CancelledError:\n            pass # Expected when cancelling\n        await worker.shutdown()\n\nif __name__ == \"__main__\":\n    asyncio.run(run_orchestration_sample())","lang":"python","description":"This quickstart demonstrates how to set up `durabletask-azuremanaged` by configuring an `AzureManagedTaskHub` using environment variables. It then defines a simple orchestrator and activity, initializes a `Worker` to process them, and uses a `TaskHubClient` to schedule and monitor an orchestration. Ensure `AZURE_DURABLETASK_CONNECTION_STRING` and `AZURE_DURABLETASK_HUB_NAME` are set in your environment before running."},"warnings":[{"fix":"Ensure both `durabletask` and `durabletask-azuremanaged` are installed at compatible `1.x` versions or newer (e.g., `pip install durabletask durabletask-azuremanaged`).","message":"The `durabletask` SDK underwent significant breaking changes leading up to its `1.0.0` release. `durabletask-azuremanaged` versions 1.x are specifically designed for `durabletask` 1.x and are not compatible with older `durabletask` 0.x SDK versions.","severity":"breaking","affected_versions":"<1.0.0 (durabletask-azuremanaged) vs. <1.0.0 (durabletask)"},{"fix":"Choose a descriptive and globally unique `task_hub_name`. Consider including project, environment, and region in the name (e.g., `MyProjectDevEastUS`).","message":"The `task_hub_name` provided to `AzureManagedTaskHub` must be globally unique within a specific Azure region if using public endpoints. Reusing names can lead to conflicts, unexpected behavior, or data corruption.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure your worker process(es) are deployed, running, and properly initialized to poll the task hub for new tasks. For development, run your worker in a separate thread or process, or alongside your client for testing.","message":"Orchestrations and activities will not execute or progress if a `durabletask.worker.Worker` instance configured with the same `AzureManagedTaskHub` is not actively running and connected.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Store `AZURE_DURABLETASK_CONNECTION_STRING` securely using environment variables, Azure Key Vault, or managed identities for production deployments. Avoid hardcoding credentials in source code.","message":"Azure connection strings for the Durable Task Hub typically include shared access keys. Managing these securely, for example, via Azure Key Vault or environment variables, is crucial. Hardcoding them is a security risk.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install durabletask-azuremanaged`.","cause":"The `durabletask-azuremanaged` library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'durabletask_azuremanaged'"},{"fix":"Verify the `AZURE_DURABLETASK_CONNECTION_STRING` and `AZURE_DURABLETASK_HUB_NAME` environment variables or directly provided values. Ensure the connection string grants appropriate permissions and your network allows outbound connections to Azure.","cause":"The provided Azure connection string is invalid, the `task_hub_name` is incorrect, or there are network/firewall issues preventing access to the Azure Durable Task backend.","error":"durabletask.exceptions.TaskHubError: Failed to connect to Azure Durable Task Hub. Please check connection string and task hub name."},{"fix":"Ensure all data passed to and from orchestrators and activities consists of JSON-serializable types (e.g., strings, numbers, lists, dictionaries, booleans, None). Convert custom objects to a serializable format (like a dict) before passing them.","cause":"Inputs and outputs for orchestrations and activities in Durable Task must be JSON-serializable. Custom Python objects are not automatically handled.","error":"TypeError: Object of type <YourCustomClass> is not JSON serializable"}]}