This is an example of how it should work:
import asyncio list_1 = 'hi i am cool'.split() async def hello(a): await asyncio.sleep(2) print(a) async def run_tasks(): tasks = [hello(word) for word in list_1] await asyncio.wait(tasks) def main(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(run_tasks()) loop.close() main() Sample output:
am i cool hi The above code is mainly for demonstration but the new and easier way to do so is:
def main2(): asyncio.run(run_tasks()) main2() Sample output:
i hi cool am Note:
as @GinoMempinAs suggested in the comments, to preserve the order of inputs, define get_tasksrun_tasks as above:
async def run_tasks(): tasks = [hello(word) for word in list_1] await asyncio.gather(*tasks)