python - 将 args、kwargs 传递给 run_in_executor

标签 python python-asyncio

我正在尝试将参数传递给 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/

相关文章:

python - 如何捕获并发.futures._base.TimeoutError

python - asyncio.sleep 如何处理负值?

Python:如何在另一个指定的字符串之后立即提取一个字符串

python - 将python路径放在windows机器上

python - 并行化使用 rpy2 的 python 代码的最有效方法是什么?

python - PyQT崩溃(底层C/C++对象已被删除) “after” clear QTreeWidget

python - 如何平移/拖动/移动QGraphicsScene?

python - python asyncio 中的 Joinable PriorityQueue

python - 在 fixture 中包含代码后,带有异步的 pytest 中的 AttributeError

python - 在 python 中将我自己的函数作为 asyncio 函数