From 0b1d9a0493fca4dfeed9d6b6054d6b942f1b66f4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 2 May 2014 02:47:54 +0300 Subject: [PATCH] asyncio_slow: Add Future examples from docs. --- asyncio_slow/test_future.py | 15 +++++++++++++++ asyncio_slow/test_future2.py | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 asyncio_slow/test_future.py create mode 100644 asyncio_slow/test_future2.py diff --git a/asyncio_slow/test_future.py b/asyncio_slow/test_future.py new file mode 100644 index 00000000..53026c8d --- /dev/null +++ b/asyncio_slow/test_future.py @@ -0,0 +1,15 @@ +#https://docs.python.org/3.4/library/asyncio-task.html#example-chain-coroutines +#import asyncio +import asyncio_slow as asyncio + +@asyncio.coroutine +def slow_operation(future): + yield from asyncio.sleep(1) + future.set_result('Future is done!') + +loop = asyncio.get_event_loop() +future = asyncio.Future() +asyncio.Task(slow_operation(future)) +loop.run_until_complete(future) +print(future.result()) +loop.close() diff --git a/asyncio_slow/test_future2.py b/asyncio_slow/test_future2.py new file mode 100644 index 00000000..8ba03ef8 --- /dev/null +++ b/asyncio_slow/test_future2.py @@ -0,0 +1,21 @@ +#https://docs.python.org/3.4/library/asyncio-task.html#example-future-with-run-forever +#import asyncio +import asyncio_slow as asyncio + +@asyncio.coroutine +def slow_operation(future): + yield from asyncio.sleep(1) + future.set_result('Future is done!') + +def got_result(future): + print(future.result()) + loop.stop() + +loop = asyncio.get_event_loop() +future = asyncio.Future() +asyncio.Task(slow_operation(future)) +future.add_done_callback(got_result) +try: + loop.run_forever() +finally: + loop.close() \ No newline at end of file