小编mik*_*key的帖子

python3.8 RuntimeError:没有运行事件循环

我从作者 caleb hattingh 的书中摘取了以下代码片段。我尝试运行代码片段并遇到此错误。(练习)

我该如何解决这个问题?

import asyncio

async def f(delay):
    await asyncio.sleep(1 / delay)
    return delay

loop = asyncio.get_event_loop()
for i in range(10):
    loop.create_task(f(i))

print(loop)
pending = asyncio.all_tasks()
group = asyncio.gather(*pending, return_exceptions=True)
results = loop.run_until_complete(group)
print(f'Results: {results}')
loop.close()
Run Code Online (Sandbox Code Playgroud)

python python-asyncio python-3.8

3
推荐指数
1
解决办法
6004
查看次数

标签 统计

python ×1

python-3.8 ×1

python-asyncio ×1