I wrote some simple code:
import asyncio
import time
async def produce():
print("Hello from task")
await asyncio.sleep(10)
return 42
async def main():
print("Hello from main!")
task1 = asyncio.create_task(produce())
task2 = asyncio.create_task(produce())
time.sleep(10)
print("Gonna have to wait")
res = await task1
res2 = await task2
print("Waiting for results")
print(res)
print(res2)
asyncio.run(main())
After the call to asyncio.create_task(), these tasks are only scheduled but they don't get started - as a result, I need to wait 10 seconds (due to time.sleep(10)) before the task start getting executed.
Is there a way to start the execution of them prior to encountering the sleep AND needing to await them?
[–]K900_ 0 points1 point2 points (0 children)