获取以下代码的错误。感谢您的帮助!
async def numbers(num):
for i in range(num):
yield i
await asyncio.sleep(0.5)
async def check_odd(num):
it = aiter(numbers(num))
while True:
x = await anext(it, 'end')
if x == 'end':
break
elif x % 2 != 0:
print(f"{x} is Odd!!")
if __name__ == " __main__":
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(check_odd(10))
finally:
event_loop.close()
nest_asyncio.apply()
asyncio.run(check_odd(10))
出现错误
Traceback (most recent call last)
<ipython-input-108-1f10e3e2fb22> in <module>()
25
26 nest_asyncio.apply()
---> 27 asyncio.run(check_odd(10))
4 frames
<ipython-input-108-1f10e3e2fb22> in check_odd(num)
9
10 async def check_odd(num):
---> 11 it = aiter(numbers(num))
12 while True:
13 x = await anext(it, 'end')
NameError: name 'aiter' is not defined
根据发布通知的这一部分,内置函数aiter
及其相关好友anext
(也在提供的代码中(仅在Python 3.10之后引入。将其更改为使用非异步对应程序(即,如果可能,使用iter
/next
(,或者使用Python 3.10或更高版本来运行相关程序。aiter
上的相关线程。