Skip to content

Instantly share code, notes, and snippets.

@lmazuel
Last active December 22, 2017 01:54
Show Gist options
  • Save lmazuel/69a0372cd4bc80417fa0a079cbde9c16 to your computer and use it in GitHub Desktop.
Save lmazuel/69a0372cd4bc80417fa0a079cbde9c16 to your computer and use it in GitHub Desktop.
Async sandbox
async def mymethod():
account_name = self.get_resource_name('pyarmstorage18')
# Normal call, async is just HTTP I/O
result_check = await self.storage_client.storage_accounts.check_name_availability(
account_name
)
self.assertTrue(result_check.name_available)
params_create = models.StorageAccountCreateParameters(
sku=models.Sku(models.SkuName.standard_lrs),
kind=models.Kind.storage,
location=location,
)
# "Create" returns a sub-class of asyncio.Future
# Create is NOT an async method. This is a regular method that returns a scheduled Future
poller = await self.storage_client.storage_accounts.create(
resource_group.name,
account_name,
params_create,
)
# We get the result
storage_account = poller.result()
# Example of non-direct await usage of the poller
poller = self.storage_client.storage_accounts.create(
resource_group.name,
account_name,
params_create,
)
# Example of interface. This is pure asyncio, and nothing is msrestazure specific
poller.cancel() # Stop polling
poller.done() # Is this done?
cb = lambda x: pass
poller.add_done_callback(cb) # Execute this callback when it's done
poller.remove_done_callback(cb)
poller.cancelled() # Was is cancelled?
poller.exception() # What was the exception?
# We list using async for
async for account in self.storage_client.list_by_resource_group(resource_group.name):
print(account.name)
# Normal wait with no result
await self.storage_client.storage_accounts.delete(
resource_group.name,
account_name,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment