将 args、kwargs 传递给 run_in_executor

2023-12-19

我正在尝试将参数传递给run_in_executor像这样:

    loop.run_in_executor(None, update_contacts, data={
        'email': email,
        'access_token': g.tokens['access_token']
    })

但是,我收到以下错误:

run_in_executor() 收到意外的关键字参数“data”

有没有通用的方法将参数传递给这个函数?


Use functools.partial;这是执行此类操作的标准方法,并且特别推荐在the docs https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor for loop.run_in_executor,以及更一般地在事件循环文档 https://docs.python.org/3/library/asyncio-eventloop.html#asyncio-pass-keywords.

对于您来说,它可能是这样的:

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, 如果你喜欢。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 args、kwargs 传递给 run_in_executor 的相关文章

随机推荐