您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 Python 的 Azure Batch 库Azure Batch libraries for python

概述Overview

使用 Azure Batch 在云中有效运行大规模并行和高性能计算应用程序。Run large-scale parallel and high-performance computing applications efficiently in the cloud with Azure Batch.

若要开始使用 Azure Batch,请参阅使用 Azure 门户创建 Batch 帐户To get started with Azure Batch, see Create a Batch account with the Azure portal.

安装库Install the libraries

客户端库Client library

使用 Azure Batch 客户端库可以配置计算节点和池、定义任务并将其配置为在作业中运行,以及设置一个作业管理器来控制和监视作业执行。The Azure Batch client libraries let you configure compute nodes and pools, define tasks and configure them to run in jobs, and set up a job manager to control and monitor job execution. 详细了解如何使用这些对象来运行大规模并行计算解决方案。Learn more about using these objects to run large-scale parallel compute solutions.

pip install azure-batch

示例Example

在 Batch 帐户中设置 Linux 计算节点池:Set up a pool of Linux compute nodes in a batch account:

# create the batch client for an account using its URI and keys
creds = batchauth.SharedKeyCredentials(account, key)
config = batch.BatchServiceClientConfiguration(creds, base_url = batch_url)
client = batch.BatchServiceClient(config)

# Create the VirtualMachineConfiguration, specifying
# the VM image reference and the Batch node agent to
# be installed on the node.
vmc = batchmodels.VirtualMachineConfiguration(
    image_reference = ir,
    node_agent_sku_id = "batch.node.ubuntu 14.04")

# Assign the virtual machine configuration to the pool
new_pool.virtual_machine_configuration = vmc

# Create pool in the Batch service
client.pool.add(new_pool)

管理 APIManagement API

使用 Azure Batch 管理库可以创建和删除 Batch 帐户、读取和重新生成 Batch 帐户密钥,以及管理 Batch 帐户存储。Use the Azure Batch management libraries to create and delete batch accounts, read and regenerate batch account keys, and manage batch account storage.

pip install azure-mgmt-batch

示例Example

创建 Azure Batch 帐户并为其配置新的应用程序和 Azure 存储帐户。Create an Azure Batch account and configure a new application and Azure storage account for it.

from azure.mgmt.batch import BatchManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.storage import StorageManagementClient

LOCATION ='eastus'
GROUP_NAME ='batchresourcegroup'
STORAGE_ACCOUNT_NAME ='batchstorageaccount'

# Create Resource group
print('Create Resource Group')
resource_client.resource_groups.create_or_update(GROUP_NAME, {'location': LOCATION})

# Create a storage account
storage_async_operation = storage_client.storage_accounts.create(
    GROUP_NAME,
    STORAGE_ACCOUNT_NAME,
    StorageAccountCreateParameters(
        sku=Sku(SkuName.standard_ragrs),
        kind=Kind.storage,
        location=LOCATION
    )
)
storage_account = storage_async_operation.result()

# Create a Batch Account, specifying the storage account we want to link
storage_resource = storage_account.id
batch_account = azure.mgmt.batch.models.BatchAccountCreateParameters(
    location=LOCATION,
    auto_storage=azure.mgmt.batch.models.AutoStorageBaseProperties(storage_resource)
)
creating = batch_client.account.create('MyBatchAccount', LOCATION, batch_account)
creating.wait()