Reputation: 8915
How to create a recursive coroutine function in python?
import asyncio
def fetch_url(x):
if isinstance(x, list):
return map(fetch_url, x)
else:
return parsed_url(x)
That is when double(10)
is called the function will be executed synchronously and whilst when double([2,3,4])
is called it would execute asynchronously.
Upvotes: 1
Views: 2153
Reputation: 30472
If by "would execute asynchronously" you mean "in parallel", try this:
async def fetch_url(x):
if isinstance(x, list):
return await asyncio.gather(*[fetch_url(url) for url in x])
return parsed_url(x)
Upvotes: 4