I was looking at some python tutorial for asyncio
package. A paradigmatic example is the following:
import asyncio
async def first_function():
print("Before")
await asyncio.sleep(1)
print("After")
async def second_function():
print("Hello")
async def main():
await asyncio.gather(first_function(), second_function())
if __name__ == "__main__":
asyncio.run(main())
Even in more complex examples, the innermost async function one is awaiting is either asyncio.sleep
or some function coming from some "async library". But how are these function made of in terms of non-async functions?
To me more concrete: suppose I have a non-async function coming from an external library whose execution time is quite long long_function
. (This function do not contain any await, it may be written in another language, for example). How can I write an async function wrapper out of it (maybe with a future) so that I can write
import asyncio
async def long_function_wrapper():
...
#something with long_function()
...
async def first_function():
print("Before")
await long_functin_wrapper()
print("After")
async def second_function():
print("Hello")
async def main():
await asyncio.gather(waiting_function(), short_function())
if __name__ == "__main__":
asyncio.run(main())
and the behavior is the same as in the former example? (i.e. the order of the output is the same)