There is an example: main
coroutine creates coroutines that take a long time to complete, what means FIRST_COMPLETED
case is unreachable. Problem: considering that await asyncio.wait(tasks)
line blocks everything under itself, how to get access to pending futures set?
import asyncio
async def worker(i):
#some big work
await asyncio.sleep(100000)
async def main():
tasks = [asyncio.create_task(worker(i), name=str(i)) for i in range(5)]
done, pending = await asyncio.wait(tasks) #or asyncio.as_completed(tasks, return_when=FIRST_COMPLETED) no matter
# everything below is unreachable until tasks are in process
# we want to kill certain task
for future in pending:
if future.get_name == "4":
future.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
How to avoid await block and kill certain coroutine? for example 4?