適用於 Python 的 Azure Batch 程式庫
概觀
使用 Azure Batch 在雲端有效地執行大規模的平行和高效能計算應用程式。
若要開始使用 Azure Batch,請參閱使用 Azure 入口網站建立 Batch 帳戶。
安裝程式庫
用戶端程式庫
Azure Batch 用戶端程式庫可讓您設定計算節點和集區、定義工作並將其設定為在作業中執行,以及設定作業管理員以控制和監控作業的執行。 深入了解如何使用這些物件以執行大規模的平行計算解決方案。
pip install azure-batch
範例
在 Batch 帳戶中設定 Linux 計算節點集區:
import azure.batch
from azure.batch import batch_auth, BatchServiceClient, models
# create the batch client for an account using its URI and keys
creds = batch_auth.SharedKeyCredentials(account, key)
client = BatchServiceClient(creds, batch_url)
# Create the VirtualMachineConfiguration, specifying
# the VM image reference and the Batch node agent to
# be installed on the node.
vmc = models.VirtualMachineConfiguration(
image_reference = models.ImageReference(
publisher='Canonical',
offer='UbuntuServer',
sku='18.04-LTS'
),
node_agent_sku_id = "batch.node.ubuntu 18.04")
# Assign the virtual machine configuration to the pool
new_pool = models.PoolAddParameter(
id = 'new_pool',
vm_size='standard_d2_v2',
virtual_machine_configuration = vmc
)
# Create pool in the Batch service
client.pool.add(new_pool)
管理 API
使用 Azure Batch 管理程式庫來建立和刪除 Batch 帳戶、讀取和重新產生 Batch 帳戶金鑰,以及管理 Batch 帳戶的儲存體。
pip install azure-mgmt-batch
範例
建立 Azure Batch 帳戶,並為其設定新的應用程式和 Azure 儲存體帳戶。
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_parameters = azure.mgmt.batch.models.BatchAccountCreateParameters(
location=LOCATION,
auto_storage=azure.mgmt.batch.models.AutoStorageBaseProperties(storage_resource)
)
creating = batch_client.batch_account.begin_create('MyBatchResourceGroup', 'MyBatchAccount', batch_account_parameters)
creating.wait()