Last active
March 10, 2019 09:15
-
-
Save kissofjudase23/c981c75ec7be32cf7a9432568bac85ee to your computer and use it in GitHub Desktop.
asyncio, use ensure_future to create task
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 time | |
| import asyncio | |
| now = lambda : time.time() | |
| start = now() | |
| async def do_some_work(x): | |
| print('Waiting: ', x) | |
| return 'Done after {}s'.format(x) | |
| def callback(future): | |
| print('Callback: ', future.result()) | |
| coroutine = do_some_work(2) | |
| loop = asyncio.get_event_loop() | |
| task = asyncio.ensure_future(coroutine) | |
| task.add_done_callback(callback) | |
| loop.run_until_complete(task) | |
| print('Task result: ', task.result()) | |
| print('TIME: ', now() - start) |
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 | |
| import time | |
| now = lambda: time.time() | |
| async def do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| async def main(): | |
| coroutine1 = do_some_work(1) | |
| coroutine2 = do_some_work(2) | |
| coroutine3 = do_some_work(4) | |
| tasks = [ | |
| asyncio.ensure_future(coroutine1), | |
| asyncio.ensure_future(coroutine2), | |
| asyncio.ensure_future(coroutine3) | |
| ] | |
| for task in asyncio.as_completed(tasks): | |
| result = await task | |
| print('Task ret: {}'.format(result)) | |
| start = now() | |
| loop = asyncio.get_event_loop() | |
| done = loop.run_until_complete(main()) | |
| print('TIME: ', now() - start) |
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 | |
| import time | |
| now = lambda: time.time() | |
| async def do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| async def main(): | |
| coroutine1 = do_some_work(1) | |
| coroutine2 = do_some_work(2) | |
| coroutine3 = do_some_work(4) | |
| tasks = [ | |
| asyncio.ensure_future(coroutine1), | |
| asyncio.ensure_future(coroutine2), | |
| asyncio.ensure_future(coroutine3) | |
| ] | |
| for task in asyncio.as_completed(tasks): | |
| await task | |
| print(task) | |
| return "main task has been done" | |
| start = now() | |
| loop = asyncio.get_event_loop() | |
| main_task = asyncio.ensure_future(main()) | |
| result = loop.run_until_complete(main_task) | |
| print(result) | |
| print('TIME: ', now() - start) | |
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 | |
| import time | |
| now = lambda: time.time() | |
| async def do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| async def main(): | |
| coroutine1 = do_some_work(1) | |
| coroutine2 = do_some_work(2) | |
| coroutine3 = do_some_work(4) | |
| tasks = [ | |
| asyncio.ensure_future(coroutine1), | |
| asyncio.ensure_future(coroutine2), | |
| asyncio.ensure_future(coroutine3) | |
| ] | |
| dones, pendings = await asyncio.wait(tasks) | |
| for future in asyncio.as_completed(dones): | |
| print(future.result()) | |
| return "main task has been done" | |
| start = now() | |
| loop = asyncio.get_event_loop() | |
| main_task = asyncio.ensure_future(main()) | |
| result = loop.run_until_complete(main_task) | |
| print(result) | |
| print('TIME: ', now() - start) |
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 do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| coroutine = do_some_work(2) | |
| loop = asyncio.get_event_loop() | |
| task = asyncio.ensure_future(coroutine) | |
| loop.run_until_complete(task) | |
| print('Task result: ', task.result()) | |
| loop.close() |
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 | |
| import time | |
| now = lambda: time.time() | |
| async def do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| start = now() | |
| coroutine1 = do_some_work(1) | |
| coroutine2 = do_some_work(1) | |
| coroutine3 = do_some_work(2) | |
| tasks = [ | |
| asyncio.ensure_future(coroutine1), | |
| asyncio.ensure_future(coroutine2), | |
| asyncio.ensure_future(coroutine3) | |
| ] | |
| loop = asyncio.get_event_loop() | |
| loop.run_until_complete(asyncio.gather(*tasks)) | |
| for task in tasks: | |
| print('Task ret: ', task) | |
| print('TIME: ', now() - start) |
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 | |
| from pprint import pprint | |
| import random | |
| async def coro(tag): | |
| print(">", tag) | |
| await asyncio.sleep(random.uniform(1, 3)) | |
| print("<", tag) | |
| return tag | |
| loop = asyncio.get_event_loop() | |
| group1 = asyncio.gather(*[coro("group 1.{}".format(i)) for i in range(1, 6)]) | |
| group2 = asyncio.gather(*[coro("group 2.{}".format(i)) for i in range(1, 4)]) | |
| group3 = asyncio.gather(*[coro("group 3.{}".format(i)) for i in range(1, 10)]) | |
| all_groups = asyncio.gather(group1, group2, group3) | |
| results = loop.run_until_complete(all_groups) | |
| loop.close() | |
| pprint(results) |
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 do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| coroutine = do_some_work(2) | |
| loop = asyncio.get_event_loop() | |
| # loop.run_until_complete will invoke ensure_futre if | |
| # the input arg is coroutine | |
| result = loop.run_until_complete(coroutine) | |
| print('Task result: ', result) | |
| loop.close() |
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 | |
| import time | |
| from threading import Thread | |
| now = lambda: time.time() | |
| def start_loop(loop): | |
| asyncio.set_event_loop(loop) | |
| loop.run_forever() | |
| async def do_some_work(x): | |
| print('Waiting {}'.format(x)) | |
| await asyncio.sleep(x) | |
| print('Done after {}s'.format(x)) | |
| def more_work(x): | |
| print('More work {}'.format(x)) | |
| time.sleep(x) | |
| print('Finished more work {}'.format(x)) | |
| start = now() | |
| new_loop = asyncio.new_event_loop() | |
| t = Thread(target=start_loop, args=(new_loop,)) | |
| t.start() | |
| print('TIME: {}'.format(time.time() - start)) | |
| asyncio.run_coroutine_threadsafe(do_some_work(6), new_loop) | |
| asyncio.run_coroutine_threadsafe(do_some_work(4), new_loop) | |
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 | |
| import time | |
| now = lambda: time.time() | |
| async def do_some_work(x): | |
| print('Waiting: ', x) | |
| await asyncio.sleep(x) | |
| return 'Done after {}s'.format(x) | |
| start = now() | |
| coroutine1 = do_some_work(1) | |
| coroutine2 = do_some_work(1) | |
| coroutine3 = do_some_work(2) | |
| tasks = [ | |
| asyncio.ensure_future(coroutine1), | |
| asyncio.ensure_future(coroutine2), | |
| asyncio.ensure_future(coroutine3) | |
| ] | |
| loop = asyncio.get_event_loop() | |
| # return_when=ALL_COMPLETED | |
| # FIRST_COMPLETED | |
| # FIRST_EXCEPTION | |
| # ALL_COMPLETED | |
| dones, pendings = loop.run_until_complete(asyncio.wait(tasks)) | |
| for task in dones: | |
| print('Task ret: ', task.result()) | |
| print('TIME: ', now() - start) |
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 | |
| import random | |
| async def coro(tag): | |
| print(">", tag) | |
| await asyncio.sleep(random.uniform(0.5, 5)) | |
| print("<", tag) | |
| return tag | |
| loop = asyncio.get_event_loop() | |
| tasks = [coro(i) for i in range(1, 11)] | |
| print("Get first result:") | |
| finished, unfinished = loop.run_until_complete( | |
| asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)) | |
| for task in finished: | |
| print(task.result()) | |
| print("unfinished:", len(unfinished)) | |
| print("Get more results in 2 seconds:") | |
| finished2, unfinished2 = loop.run_until_complete( | |
| asyncio.wait(unfinished, timeout=2)) | |
| for task in finished2: | |
| print(task.result()) | |
| print("unfinished2:", len(unfinished2)) | |
| print("Get all other results:") | |
| finished3, unfinished3 = loop.run_until_complete(asyncio.wait(unfinished2)) | |
| for task in finished3: | |
| print(task.result()) | |
| loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment