Skip to content

Instantly share code, notes, and snippets.

@hclivess
Last active June 20, 2022 01:05
Show Gist options
  • Save hclivess/e389dc75864e96a130bb65844fe3ac5d to your computer and use it in GitHub Desktop.
Save hclivess/e389dc75864e96a130bb65844fe3ac5d to your computer and use it in GitHub Desktop.
Simplest parallel asyncio
import asyncio
import time
async def function1():
await asyncio.sleep(2)
print("a")
async def function2():
print("b")
async def main():
await asyncio.gather(function1(),
function2())
if __name__ == "__main__":
asyncio.run(main())
@hclivess
Copy link
Author

hclivess commented Jun 20, 2022

alternative:

import asyncio
import time

async def run_parallel(*functions):
    await asyncio.gather(*functions)

async def function1():
    await asyncio.sleep(2)
    print("a")

async def function2():
    print("b")

async def main():
    await run_parallel(function1(),
                        function2())

if __name__ == "__main__":
    asyncio.run(main())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment