我正在通过默认的 asyncio
事件循环运行函数 provision_ec2_node()
thread executor 。该函数接受许多参数,我通过 functools.partial() 传递给执行器。
task = loop.run_in_executor(
executor=None,
callback=functools.partial(
provision_ec2_node,
modules=modules,
host=instance.ip_address,
identity_file=identity_file,
cluster_info=cluster_info))
这段代码在 Python 3.4 上运行良好,我已经这样使用了几个月了。
但是,我最近升级到了 Python 3.5,现在上面的代码抛出了这个错误:
TypeError: run_in_executor() got an unexpected keyword argument 'callback'
查看Python 3.5 release notes concerning asyncio
,我没有看到任何可以解释这种行为变化的内容。此外,3.5 文档 still say functools.partial()
是将带有关键字的函数传递给执行器的正确方法。
什么给出了?
最佳答案
显然第二个参数是 renamed从 callback
到 func
,但更改未反射(reflect)在文档中 截至 2015 年 10 月 1 日,更改已反射(reflect)在文档中。这就是它失败的原因。
要么将其更新为新名称(并失去 Python <3.5 兼容性),要么将参数作为位置参数传递:
task = loop.run_in_executor(None, functools.partial(...))
关于python - BaseEventLoop.run_in_executor() 抛出 "unexpected keyword argument ' 回调'"从 Python 3.5 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873974/