Wrapping a coroutine inside a function [duplicate]

@deceze answer is probably the best you can do in Python 3.6. But in Python 3.7, you could directly use asyncio.run in the following way:

newfeature = asyncio.run(main(urls))

It will properly create, handle, and close an event_loop.


You would use an event loop to execute the asynchronous function to completion:

newfeature = asyncio.get_event_loop().run_until_complete(main(urls_and_coords))

(This technique is already used inside main. And I'm not sure why, since main is async you could/should use await fetch_all(...) there.)


Another option that could be useful is the syncer PyPI package:

from syncer import sync

@sync
async def print_data():
    print(await get_data())

print_data()  # Can be called synchronously