gpt4 book ai didi

python 3.6 和 ValueError : loop argument must agree with Future

转载 作者:太空狗 更新时间:2023-10-29 18:27:12 24 4
gpt4 key购买 nike

我只想运行一个简单的测试示例,但出现以下错误。我该如何解决?

import asyncio
import uvloop
import concurrent.futures
import time
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

async def do_some_work(x):

while True:
print("Waiting " + str(x))
await asyncio.sleep(x)

if __name__ == '__main__':


loop = asyncio.new_event_loop()
tasks = [asyncio.ensure_future(do_some_work(2)),
asyncio.ensure_future(do_some_work(5))]
loop.run_until_complete(asyncio.gather(*tasks))

Traceback (most recent call last):
File "/Users/worker_why.py", line 23, in <module>
loop.run_until_complete(asyncio.gather(*tasks))
File "uvloop/loop.pyx", line 1181, in uvloop.loop.Loop.run_until_complete (uvloop/loop.c:25184)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 508, in ensure_future
raise ValueError('loop argument must agree with Future')
ValueError: loop argument must agree with Future

最佳答案

loop = asyncio.new_event_loop()

如果您创建新的事件循环并且不想将它传递到任何地方,您还应该使这个循环成为上下文的当前循环:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

关于python 3.6 和 ValueError : loop argument must agree with Future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com