Let's say I have a class which uses asyncio loop internally and doesn't have async interface:
class Fetcher:
_loop = None
def get_result(...):
"""
After 3 nested sync calls async tasks are finally called with *run_until_complete*
"""
...
I use all advantages of asyncio internally and don't have to care about it in the outer code.
But then I want to call 3 Fetcher
instances in one event loop. If I had async def
interface there would be no problem: asyncio.gather
could help me. Is there really no other way to do it without supporting both interfaces? Come on! It makes you change all your project because of one asyncio usage. Tell me this is not true.
It's true
Whole idea of using
await
keyword is to execute concurrent jobs in one event loop from different places of the code (which you can't do with regular function code).asyncio
- is not some utility, but whole style of writing asynchronous programs.On the other hand Python is very flexible, so you can still try to hide using of asyncio. If you really want to get sync result of 3 Fetcher instances, you can for example do something like this:
Output:
But, again, you'll really regret someday if you continue to write code this way, believe me. If you want to get full advantage of asynchronous programming - use
coroutines
andawait
explicitly.