python 3.6和ValueError:循环参数必须与Future一致
Posted
技术标签:
【中文标题】python 3.6和ValueError:循环参数必须与Future一致【英文标题】:python 3.6 and ValueError: loop argument must agree with Future 【发布时间】:2017-10-18 08:48:52 【问题描述】:我只想运行一个简单的测试示例,但出现以下错误。我该如何解决?
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
【问题讨论】:
您是否尝试过将新创建的循环传递给ensure_future
? asyncio.ensure_future(..., loop=loop)
【参考方案1】:
loop = asyncio.new_event_loop()
如果您创建新的事件循环并且不想到处传递它,您还应该将此循环设为当前上下文:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
【讨论】:
以上是关于python 3.6和ValueError:循环参数必须与Future一致的主要内容,如果未能解决你的问题,请参考以下文章
是否可以使用python 3.6中的循环创建从1到n的数字列表,然后打印出来?
在 Tensorflow 中实现自定义损失函数会导致“ValueError:必须在循环之前定义‘输出’。”
选择KBest错误:ValueError:未知标签类型:(数组([0.55,0.84,0.72,0.54,0.59,0.77,0.85,1.03,1.62,3.04,3.6]),)