Issue
I'm doing few API calls towards Azure REST API using asyncio and aiohttp, the problem is output of each API call which is written to variable is replacing the output of the previous call. Any ideas how to fix it?
async def TM_main(session, urlTMs):
async with session.get(urlTMs) as resp:
getTMs = await resp.json()
return getTMs
async def TrafficManagers():
async with aiohttp.ClientSession(headers=headerAuth,connector=aiohttp.TCPConnector(ssl=False)) as session:
tasks = []
for subscription in subscriptions['value']:
urlTMs = 'https://management.azure.com/subscriptions/'+subscription['subscriptionId']+'/providers/Microsoft.Network/trafficmanagerprofiles?api-version=2018-04-01&$top=2000'
tasks.append(asyncio.ensure_future(TM_main(session,urlTMs)))
getTMs = await asyncio.gather(*tasks)
Solution
"...the problem is output of each API call which is written to variable is replacing the output of the previous call"
No it's not. await
ing the gather()
coroutine would return a list of all values returned by passed awaitable objects.
Your setup looks ok to me. For creating tasks it's recommended to use create_task()
instead of ensure_future()
:
Important See also the
create_task()
function which is the preferred way for creating new Tasks.
Answered By - S.B
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.