Доброго времени суток. При попытке вывести 4 простых функции через asyncio, выдает ошибки описанные ниже.
Код:
Вывод:
Factorial [<coroutine object factorials at 0x000002D7A17A8D60>, <coroutine object factorials at 0x000002D7A17A9000>, <coroutine object factorials at 0x000002D7A1800120>, <coroutine object factorials at 0x000002D7A18003C0>, <coroutine object factorials at 0x000002D7A18028F0>, <coroutine object factorials at 0x000002D7A1802960>, <coroutine object factorials at 0x000002D7A1802C70>, <coroutine object factorials at 0x000002D7A1802CE0>, <coroutine object factorials at 0x000002D7A1802D50>, <coroutine object factorials at 0x000002D7A1802DC0>]
Fibonacci list: [1, 10, 11, 21, 32, 53, 85, 138, 223, 361]
Squares [<coroutine object squares.<locals>.square at 0x000002D7A1803060>, <coroutine object squares.<locals>.square at 0x000002D7A18030D0>, <coroutine object squares.<locals>.square at 0x000002D7A1803140>, <coroutine object squares.<locals>.square at 0x000002D7A18031B0>, <coroutine object squares.<locals>.square at 0x000002D7A1803220>, <coroutine object squares.<locals>.square at 0x000002D7A1803290>, <coroutine object squares.<locals>.square at 0x000002D7A1803300>, <coroutine object squares.<locals>.square at 0x000002D7A1803370>, <coroutine object squares.<locals>.square at 0x000002D7A18033E0>, <coroutine object squares.<locals>.square at 0x000002D7A1803450>]
RuntimeWarning: coroutine 'squares.<locals>.square' was never awaited
print('Squares', list(map(square, range(1, n + 1))))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'cubics.<locals>.cubic' was never awaited
print('Cubics', list(map(cubic, range(1, n + 1))))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'cubics.<locals>.cubic' was never awaited
handle = None # Needed to break cycles when an exception occurs.
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'squares.<locals>.square' was never awaited
handle = None # Needed to break cycles when an exception occurs.
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
sys:1: RuntimeWarning: coroutine 'factorials' was never awaited
Cubics [<coroutine object cubics.<locals>.cubic at 0x000002D7A1803450>, <coroutine object cubics.<locals>.cubic at 0x000002D7A18037D0>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1803840>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1803A70>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889540>, <coroutine object cubics.<locals>.cubic at 0x000002D7A18895B0>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889620>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889690>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889700>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889770>]
Process finished with exit code 0
Код:
Python:
import asyncio
async def fibonacci(n):
if n <= 0:
return "Incorrect Output"
datas = [1, 10]
if n > 2:
for i in range(2, n):
datas.append(datas[i - 1] + datas[i - 2])
print('Fibonacci list:', datas)
return datas[n - 1]
async def factorials(n):
fact = 1
for factor in range(1, n + 1):
fact *= factor
return fact
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_factorials = [factorials(x) for x in data]
print('Factorial', my_factorials)
async def squares(n):
async def square(n):
return await n ** 2
if n < 0:
return []
else:
print('Squares', list(map(square, range(1, n + 1))))
return list(map(square, range(1, n + 1)))
async def cubics(n):
async def cubic(n):
return await n ** 3
if n < 0:
return []
else:
print('Cubics', list(map(cubic, range(1, n + 1))))
return list(map(cubic, range(1, n + 1)))
async def main():
await asyncio.gather(fibonacci(10), factorials(10), squares(10), cubics(10))
if __name__ == '__main__':
asyncio.run(main())
Вывод:
Factorial [<coroutine object factorials at 0x000002D7A17A8D60>, <coroutine object factorials at 0x000002D7A17A9000>, <coroutine object factorials at 0x000002D7A1800120>, <coroutine object factorials at 0x000002D7A18003C0>, <coroutine object factorials at 0x000002D7A18028F0>, <coroutine object factorials at 0x000002D7A1802960>, <coroutine object factorials at 0x000002D7A1802C70>, <coroutine object factorials at 0x000002D7A1802CE0>, <coroutine object factorials at 0x000002D7A1802D50>, <coroutine object factorials at 0x000002D7A1802DC0>]
Fibonacci list: [1, 10, 11, 21, 32, 53, 85, 138, 223, 361]
Squares [<coroutine object squares.<locals>.square at 0x000002D7A1803060>, <coroutine object squares.<locals>.square at 0x000002D7A18030D0>, <coroutine object squares.<locals>.square at 0x000002D7A1803140>, <coroutine object squares.<locals>.square at 0x000002D7A18031B0>, <coroutine object squares.<locals>.square at 0x000002D7A1803220>, <coroutine object squares.<locals>.square at 0x000002D7A1803290>, <coroutine object squares.<locals>.square at 0x000002D7A1803300>, <coroutine object squares.<locals>.square at 0x000002D7A1803370>, <coroutine object squares.<locals>.square at 0x000002D7A18033E0>, <coroutine object squares.<locals>.square at 0x000002D7A1803450>]
RuntimeWarning: coroutine 'squares.<locals>.square' was never awaited
print('Squares', list(map(square, range(1, n + 1))))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'cubics.<locals>.cubic' was never awaited
print('Cubics', list(map(cubic, range(1, n + 1))))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'cubics.<locals>.cubic' was never awaited
handle = None # Needed to break cycles when an exception occurs.
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
RuntimeWarning: coroutine 'squares.<locals>.square' was never awaited
handle = None # Needed to break cycles when an exception occurs.
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
sys:1: RuntimeWarning: coroutine 'factorials' was never awaited
Cubics [<coroutine object cubics.<locals>.cubic at 0x000002D7A1803450>, <coroutine object cubics.<locals>.cubic at 0x000002D7A18037D0>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1803840>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1803A70>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889540>, <coroutine object cubics.<locals>.cubic at 0x000002D7A18895B0>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889620>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889690>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889700>, <coroutine object cubics.<locals>.cubic at 0x000002D7A1889770>]
Process finished with exit code 0