import asyncio async def say_hi(): while True: print('hi') await asyncio.sleep(1) async def say_hello(): while True: print('Helloooooooooo') await asyncio.sleep(2) async def main(): task1 = loop.create_task(say_hi()) task2 = loop.create_task(say_hello()) await asyncio.wait([task1, task2]) """This is also correct""" # await asyncio.gather(say_hi(), say_hello()) if __name__ == '__main__': loop = asyncio.get_event_loop() try: loop.run_until_complete(main()) finally: loop.close()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter