Last active
February 11, 2020 11:12
-
-
Save Attumm/0eeffe5038f8370d48abd7912d5f5abc to your computer and use it in GitHub Desktop.
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 | |
| @asyncio.coroutine | |
| def do_three_things(n): | |
| print("{} start".format(n)) | |
| message = yield from create_message_one(n) | |
| print(message) | |
| message = yield from create_message_two(n) | |
| print(message) | |
| message = yield from create_message_three(n) | |
| print(message) | |
| @asyncio.coroutine | |
| def create_message_one(n): | |
| yield from asyncio.sleep(0) | |
| return '{} message one'.format(n) | |
| @asyncio.coroutine | |
| def create_message_two(n): | |
| yield from asyncio.sleep(0) | |
| if n > 5: | |
| yield from asyncio.sleep(0) | |
| return '{} message two'.format(n) | |
| @asyncio.coroutine | |
| def create_message_three(n): | |
| yield from asyncio.sleep(0) | |
| return '{} message three'.format(n) | |
| if __name__ == "__main__": | |
| loop = asyncio.get_event_loop() | |
| print('start') | |
| tasks = [asyncio.ensure_future(do_three_things(i)) for i in range(10)] | |
| print('set tasks') | |
| loop.run_until_complete(asyncio.wait(tasks)) | |
| print('done') |
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 | |
| async def create_message_one(n): | |
| await asyncio.sleep(0) | |
| return '{} message one'.format(n) | |
| async def create_message_two(n): | |
| if n > 5: | |
| await asyncio.sleep(0) | |
| await asyncio.sleep(0) | |
| return '{} message two'.format(n) | |
| async def create_message_three(n): | |
| await asyncio.sleep(0) | |
| return '{} message three'.format(n) | |
| async def do_three_things(n): | |
| print("{} start".format(n)) | |
| message = await create_message_one(n) | |
| print(message) | |
| message = await create_message_two(n) | |
| print(message) | |
| message = await create_message_three(n) | |
| print(message) | |
| if __name__ == "__main__": | |
| loop = asyncio.get_event_loop() | |
| print('start') | |
| tasks = [asyncio.ensure_future(do_three_things(i)) for i in range(10)] | |
| print('set tasks') | |
| loop.run_until_complete(asyncio.wait(tasks)) | |
| print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment