Python 用 Azure Batch ライブラリ

概要

Azure Batch を使用すると、大規模な並列コンピューティングやハイパフォーマンス コンピューティングのアプリケーションをクラウドで効率的に実行できます。

Azure Batch の概要については、「Azure Portal で 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

Batch アカウントの作成と削除、Batch アカウント キーの読み取りと再生成、Batch アカウントのストレージ管理は、Azure 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()