From 2feacf5a50dae48125467c1145ef210fe5df88cb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 2 May 2014 02:54:11 +0300 Subject: [PATCH] asyncio_slow: Add example of wait() from docs. --- asyncio_slow/test_parallel.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 asyncio_slow/test_parallel.py diff --git a/asyncio_slow/test_parallel.py b/asyncio_slow/test_parallel.py new file mode 100644 index 00000000..48a187b8 --- /dev/null +++ b/asyncio_slow/test_parallel.py @@ -0,0 +1,21 @@ +#https://docs.python.org/3.4/library/asyncio-task.html#example-parallel-execution-of-tasks +#import asyncio +import asyncio_slow as asyncio + +@asyncio.coroutine +def factorial(name, number): + f = 1 + for i in range(2, number+1): + print("Task %s: Compute factorial(%s)..." % (name, i)) + yield from asyncio.sleep(1) + f *= i + print("Task %s: factorial(%s) = %s" % (name, number, f)) + +tasks = [ + asyncio.Task(factorial("A", 2)), + asyncio.Task(factorial("B", 3)), + asyncio.Task(factorial("C", 4))] + +loop = asyncio.get_event_loop() +loop.run_until_complete(asyncio.wait(tasks)) +loop.close()