Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created July 4, 2017 17:22
Show Gist options
  • Save vlad-bezden/1983b3fcb4e7098e46a07bfde35715ab to your computer and use it in GitHub Desktop.
Save vlad-bezden/1983b3fcb4e7098e46a07bfde35715ab to your computer and use it in GitHub Desktop.
Example on how to run in parallel more than one task using asyncio and async/await
import asyncio
MAX_TASKS = 5
async def hello():
print('Hello')
await asyncio.sleep(3)
print('World!')
def main():
loop = asyncio.get_event_loop()
print(f'Creating {MAX_TASKS} Tasks')
tasks = [hello() for i in range(MAX_TASKS)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
print('Done')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment