我试着把论点传给run_in_executor个这样的人:

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

但是,我得到了以下错误:

run_in_executor()得到一个意外的关键字参数"data"

是否有一种通用方法将参数传递给此函数?

推荐答案

使用functools.partial;这是做这类事情的标准方法,在the docs中特别推荐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-3.x相关问答推荐

Django 5.0.2和django_rest_framework

没有这样的命令';角色';-可靠分子

在 Python 中比较和排序列之间的值(带有不匹配列)

将自定义函数应用于 pandas 数据框的每一列

如何根据索引子列表对元素列表进行分组或批处理?

它们是否同样存储在python3的内存中?

如何在 20 秒后重复使用 Pillow 在现有图像上创建新图像?

考虑到Pandas 系列中的不同索引,如何正确估计两列的百分比变化? Python相关

通过点和线计算CV2 Homography

类型提示和链式赋值以及多重赋值

有没有更好的方法来判断一个数字是否是两个数字的范围

tensorflow 中 numpy.newaxis 的替代方案是什么?

Linux Mint 上的 Python3 错误没有名为蓝牙的模块

Python 3.5:async with导致 SyntaxError.为什么?

在 Ubuntu 上为 Python3 安装 mod_wsgi

混合全局/参数和名为top的函数的奇怪python行为

python - 使用 matplotlib 和 boto 将绘图从内存上传到 s3

TypeError:只有整数标量数组可以转换为标量索引

python - Pandas - Dataframe.set_index - 如何保留旧的索引列

Python 无法处理以 0 开头的数字字符串.为什么?