0

I am new to Azure, Python and Asynchronous programming.

I have a situation where I need to do some heavy tasks (in this case, creating snapshots for Azure's managed disks)

I have something like:

def create_snapshot():
    for VM in list_of_virtual_machines:
        snapshot =  compute_client.snapshots.begin_create_or_update(VM.name, VM.location).result() # asynchronous task
        # Do something with the snapshot once it is ready, in the meantime, continue creating other virtual machines' snapshots

How do I write code for this situation, and which method should I choose? Threading, Asyncio etc. ? My question might be unclear and vague since I am new to these concepts.

Any help is appreciated. Thanks in advance.

1 Answers1

0

How do I write code for this situation, and which method should I choose? Threading, Asyncio etc. ?

You can try the following code snippet, taken from the document:

managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')

async_snapshot_creation = self.compute_client.snapshots.begin_create_or_update(
        'my_resource_group',
        'mySnapshot',
        {
            'location': 'westus',
            'creation_data': {
                'create_option': 'Copy',
                'source_uri': managed_disk.id
            }
        }
    )
snapshot = async_snapshot_creation.result()

References: snapshots_operations.py, Azure Python SDK Compute Client isn't Using Managed Disk Parameters and Scale your Python service with Managed Disks

Ecstasy
  • 1,866
  • 1
  • 9
  • 17
  • Thank you so much for your answer. But I am still confused. This snippet creates a snapshot asynchronously, but how do I make my python code also run this code asynchronously? Or is it unnecessary? – Azure Newbie Jul 18 '22 at 08:35
  • There is no need to write separate Python code. [`azure-mgmt-compute`](https://learn.microsoft.com/en-us/python/api/overview/azure/virtualmachines) library takes care of the asynchronous operation. [snapshots_operations.py](https://github.com/Azure/azure-sdk-for-python/blob/1486a398c39cf9e2f1b7aa938a3e7e52f55fd6e1/sdk/compute/azure-mgmt-compute/azure/mgmt/compute/v2022_03_02/aio/operations/_snapshots_operations.py) – Ecstasy Jul 18 '22 at 09:13