I have a pool of tasks in a list and each of those tasks take a different amount of time to complete. To mimick this I'll use this piece of code:
tasks = [asyncio.create_task(asyncio.sleep(i)) for i in range(10)]
If I use the asyncio gather API like this: await asyncio.gather(*tasks)
, then this statement blocks the event loop untill all the tasks in the current batch finishes.
The problem however is in my program, I want to execute those tasks with a ThreadPoolExecutor like interface with x
number of workers. This will allow for executing tasks more efficiently (Tasks which finish up early will be replaced by another task instead of waiting for all tasks of that batch to complete).
How can I achieve this using python asyncio?