Azure Batch Management Client Library
The Azure Batch Management Client Library for Python, currently at version 19.0.0, allows developers to programmatically manage Azure Batch accounts, pools, and jobs. It provides interfaces for creating, updating, deleting, and querying Batch resources within an Azure subscription. Releases are frequent, typically aligned with new Azure Batch service API versions and broader Azure SDK updates.
Warnings
- breaking Major version increments (e.g., 18.x.x to 19.x.x) often signify an update to a newer Azure Batch service API version. This regularly involves breaking changes to model schemas, parameter names, and client method signatures. For instance, version 19.0.0 updated to API version 2024-02-01 and included model and parameter renames.
- gotcha Authentication with `DefaultAzureCredential` can fail with `CredentialUnavailableError` if required environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET` for a Service Principal) are not set, or if you are not logged in via Azure CLI (`az login`) or other supported means.
- gotcha Many operations that modify or create resources (e.g., `create`, `update`, `delete`) are Long-Running Operations (LROs) that return a poller object. It's crucial to `wait()` on this poller (for synchronous clients) or `await` its completion (for asynchronous clients) to ensure the operation completes successfully and to retrieve the final result.
Install
-
pip install azure-mgmt-batch azure-identity
Imports
- BatchManagementClient
from azure.mgmt.batch import BatchManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.batch import BatchManagementClient
# Ensure you have your Azure Subscription ID set as an environment variable (AZURE_SUBSCRIPTION_ID)
# or replace 'YOUR_SUBSCRIPTION_ID' with your actual subscription ID.
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
if subscription_id == "YOUR_SUBSCRIPTION_ID":
print("WARNING: AZURE_SUBSCRIPTION_ID environment variable not set. Using placeholder.")
print("Please set AZURE_SUBSCRIPTION_ID for actual resource management.")
try:
# Acquire a credential object for authentication
credential = DefaultAzureCredential()
# Create a BatchManagementClient instance
batch_client = BatchManagementClient(credential, subscription_id)
print(f"Listing Batch accounts in subscription: {subscription_id}")
# List all Batch accounts in the current subscription
# This operation is synchronous.
batch_accounts = batch_client.batch_account.list_by_subscription()
found_accounts = 0
for account in batch_accounts:
print(f"- Account name: {account.name}, Location: {account.location}, Provisioning State: {account.provisioning_state.value}")
found_accounts += 1
if found_accounts == 0:
print("No Batch accounts found in this subscription.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you have set AZURE_SUBSCRIPTION_ID and are authenticated (e.g., via Azure CLI 'az login' or environment variables for a Service Principal).")