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.
CodePudding user response:
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