Skip to main content
deleted 14 characters in body
Source Link
Gino Mempin
  • 30.5k
  • 31
  • 125
  • 174

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) 

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 @GinoMempin suggested in comments, to preserve the order of inputs, define get_tasks as above:

async def run_tasks(): tasks = [hello(word) for word in list_1] await asyncio.gather(*tasks) 

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 suggested in the comments, to preserve the order of inputs, define run_tasks as:

async def run_tasks(): tasks = [hello(word) for word in list_1] await asyncio.gather(*tasks) 
added 215 characters in body
Source Link
adir abargil
  • 5.7k
  • 3
  • 23
  • 29

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 @GinoMempin suggested in comments, to preserve the order of inputs, define get_tasks as above:

async def run_tasks(): tasks = [hello(word) for word in list_1] await asyncio.gather(*tasks) 

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 

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 @GinoMempin suggested in comments, to preserve the order of inputs, define get_tasks as above:

async def run_tasks(): tasks = [hello(word) for word in list_1] await asyncio.gather(*tasks) 
Add spacing/separation for readability
Source Link
Gino Mempin
  • 30.5k
  • 31
  • 125
  • 174

thisThis 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 

theThe 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 

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() >>> 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() >>> i hi cool am 

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 
added 174 characters in body
Source Link
adir abargil
  • 5.7k
  • 3
  • 23
  • 29
Loading
Source Link
adir abargil
  • 5.7k
  • 3
  • 23
  • 29
Loading