Azure Batch Client Library for Python

14.2.0 · active · verified Sun Mar 29

The `azure-batch` client library for Python enables users to configure compute nodes and pools, define tasks, and manage jobs for large-scale parallel and high-performance computing (HPC) applications in Azure. The current stable version is 14.2.0, with ongoing development as part of the broader Azure SDK for Python, which typically sees monthly releases for various packages.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `BatchServiceClient` using the recommended `DefaultAzureCredential` from `azure-identity`. It retrieves the Batch account URL from an environment variable and then lists the existing pools. For shared key authentication, an alternative commented-out section is provided.

import os
from azure.batch import BatchServiceClient
from azure.identity import DefaultAzureCredential

# Retrieve Batch account details from environment variables
batch_account_url = os.environ.get("AZURE_BATCH_ACCOUNT_URL", "https://<your-batch-account>.westus.batch.azure.com")

# Authenticate using DefaultAzureCredential (recommended for Azure AD)
# This will attempt to authenticate via environment variables, managed identity, etc.
try:
    credential = DefaultAzureCredential()
    batch_client = BatchServiceClient(credential, batch_url=batch_account_url)
    
    # Example: List existing pools
    pools = batch_client.pool.list()
    print(f"Successfully connected to Azure Batch. Found {len(pools)} pools.")
    for pool in pools:
        print(f"  - Pool ID: {pool.id}, VM Size: {pool.vm_size}")

except Exception as e:
    print(f"Error connecting to Azure Batch: {e}")
    print("Please ensure AZURE_BATCH_ACCOUNT_URL is set and your environment is authenticated (e.g., via Azure CLI).")

# For shared key authentication (less recommended, but available):
# batch_account_name = os.environ.get("AZURE_BATCH_ACCOUNT_NAME", "<your-batch-account-name>")
# batch_account_key = os.environ.get("AZURE_BATCH_ACCOUNT_KEY", "<your-batch-account-key>")
# if batch_account_name and batch_account_key:
#     from azure.batch.batch_auth import SharedKeyCredentials
#     creds = SharedKeyCredentials(batch_account_name, batch_account_key)
#     batch_client_shared_key = BatchServiceClient(creds, batch_url=batch_account_url)
#     print("Successfully connected with Shared Key credentials.")

view raw JSON →