我正在尝试将参数传递给 run_in_executor
,如下所示:
loop.run_in_executor(None, update_contacts, data={
'email': email,
'access_token': g.tokens['access_token']
})
但是,我收到以下错误:
run_in_executor() got an unexpected keyword argument 'data'
是否有通用的方法将参数传递给此函数?
最佳答案
使用functools.partial
;这是执行此类操作的标准方法,在 the docs 中特别推荐。 for loop.run_in_executor
,以及更普遍的 the Event Loop docs .
以下是它可能适合您的方式:
import functools # at the top with the other imports
loop.run_in_executor(None, functools.partial(update_contacts, data={
'email': email,
'access_token': g.tokens['access_token']
}))
如果愿意,您也可以from functools import partial
。
关于python - 将 args、kwargs 传递给 run_in_executor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53368203/