I used asyncio.create_task()
only to realize that the task is fed to the task engine at a very late time. To make late time of execution obvious, I put some IO requests in the code below:
import asyncio
import requests
async def func2():
# always executes last
print("async asf")
def func1():
asyncio.create_task(func2())
async def main():
print("main started")
func1()
print("main ended")
# lets do some IO requests to give python plenty of time to execute func2
session = requests.Session()
response = session.get("https://youtube.com")
# this should be the last thing printed, but it is not
print(response.status_code)
if __name__ == '__main__':
asyncio.run(main())
This prints:
main started
main ended
200
async asf
Although I expected async asf
to be before 200
How do you artificially trigger task execution? Is there some semantic sugar?
source https://stackoverflow.com/questions/71959849/force-create-task-execution
Comments
Post a Comment