mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Issue #29407: Remove redundant ensure_future() calls in factorial example
This commit is contained in:
parent
137b5a2861
commit
d5adb63673
1 changed files with 7 additions and 8 deletions
|
|
@ -472,21 +472,20 @@ Example executing 3 tasks (A, B, C) in parallel::
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def factorial(name, number):
|
||||||
def factorial(name, number):
|
|
||||||
f = 1
|
f = 1
|
||||||
for i in range(2, number+1):
|
for i in range(2, number+1):
|
||||||
print("Task %s: Compute factorial(%s)..." % (name, i))
|
print("Task %s: Compute factorial(%s)..." % (name, i))
|
||||||
yield from asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
f *= i
|
f *= i
|
||||||
print("Task %s: factorial(%s) = %s" % (name, number, f))
|
print("Task %s: factorial(%s) = %s" % (name, number, f))
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
tasks = [
|
loop.run_until_complete(asyncio.gather(
|
||||||
asyncio.ensure_future(factorial("A", 2)),
|
factorial("A", 2),
|
||||||
asyncio.ensure_future(factorial("B", 3)),
|
factorial("B", 3),
|
||||||
asyncio.ensure_future(factorial("C", 4))]
|
factorial("C", 4),
|
||||||
loop.run_until_complete(asyncio.gather(*tasks))
|
))
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
Output::
|
Output::
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue