Created
July 4, 2017 17:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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