In this example, we show how three mathematical functions can be executed concurrently by the asyncio.Task() statement:
- Of course, let's start by importing the asyncio library:
import asyncio
- In the first coroutine, the factorial function is defined:
@asyncio.coroutine
def factorial(number):
f = 1
for i in range(2, number + 1):
print("Asyncio.Task: Compute factorial(%s)" % (i))
yield from asyncio.sleep(1)
f *= i
print("Asyncio.Task - factorial(%s) = %s" % (number, f))
- After which, the second function is defined—the fibonacci function:
@asyncio.coroutine
def fibonacci(number):
a, b = 0, 1
for i in range(number):
print("Asyncio.Task: Compute fibonacci (%s)" % (i))
yield from asyncio.sleep(1)
a, b = b, a + b
print("Asyncio.Task - fibonacci(%s) = %s" % (number, a))
- The last function to be executed concurrently is the binomial coefficient:
@asyncio.coroutine
def binomial_coefficient(n, k):
result = 1
for i in range(1, k + 1):
result = result * (n - i + 1) / i
print("Asyncio.Task: Compute binomial_coefficient (%s)" %
(i))
yield from asyncio.sleep(1)
print("Asyncio.Task - binomial_coefficient(%s , %s) = %s" %
(n,k,result))
- In the __main__ function, task_list contains the functions that must be performed in parallel using the asyncio.Task function:
if __name__ == '__main__':
task_list = [asyncio.Task(factorial(10)),
asyncio.Task(fibonacci(10)),
asyncio.Task(binomial_coefficient(20, 10))]
- Finally, we acquire the event loop and start the computation:
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(task_list))
loop.close()