Python용 Azure Batch 라이브러리

개요

Azure Batch를 사용하여 클라우드에서 대규모 병렬 및 고성능 컴퓨팅 애플리케이션을 효율적으로 실행합니다.

Azure Batch를 시작하려면 Azure Portal에서 Batch 계정 만들기를 참조하세요.

라이브러리 설치

클라이언트 라이브러리

Azure Batch 클라이언트 라이브러리를 사용하여 컴퓨팅 노드와 풀을 구성하고, 태스크를 정의하고 작업에서 실행되도록 구성하고, 작업 실행을 제어하고 모니터링하도록 작업 관리자를 설정할 수 있습니다. 이러한 개체를 사용하여 대규모 병렬 컴퓨팅 솔루션을 실행하는 방법에 대해 자세히 알아보세요.

pip install azure-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 Storage 계정을 구성합니다.

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()