'Asyncio in a for loop

So I am trying to do something like this,

for job in jobs:
    input = some_io_task(job)
    output_list.append(process(input))

return output_list

I want the loop to continue as the some_io_task function is being executed, and come back to the that particular iteration and then append to the output list, the order in which the append takes place does not matter, and I am trying to do this using asyncio in python. I am new to this and would really appreciate any help. Thanks!



Solution 1:[1]

Per your request, here is an example considering some_io_task returns an awaitable:

import asyncio

async def some_io_task(job):
    ...

async def stuff(jobs):
    # Create all coroutines for the jobs
    tasks = map(some_io_task, jobs)
    # Schedule all coroutines, returning each as they're ready
    for task in asyncio.as_completed(tasks):
        result = await task
        output_list.append(process(result))
    return output_list

It is extremely specific to your case. Keep in mind I'm missing some information about some_io_task but I believe this should explain it well enough.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bharel