python - 如何为 apply_async 提供 Python 请求 header ?

标签 python python-requests

我正在使用 apply_async 开发并行 GET 功能,如 Python Requests: Don't wait for request to finish 中所述。 。我的问题是我需要为每个 GET 请求提供 header ,但我不知道如何做到这一点。

我正在尝试这样做:

items.append(pool.apply_async(requests.get, [url, "", {"header1":"value1", "header2":"value2"}]))

主题的许多变体都没有成功。

我希望了解如何解决这个问题。

谢谢!

最佳答案

根据the requests docs ,您需要将 headers 关键字参数中的 header 传递给 requests.get

根据the multiprocessing docsapply_async 的参数是:

apply_async(func, args=(), kwds={}, callback=None)

在您的情况下,这将转换为:

pool.apply_async(requests.get, 
                 [url], 
                 dict(headers={"header1":"value1", "header2":"value2"}))

关于python - 如何为 apply_async 提供 Python 请求 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037122/

相关文章:

Python 请求组合 .pem 文件的 SSL 错误

python - 使用带有下拉选项的 Python 请求模块

python - 带有时区的日期时间对象

python - 由于证书过时,pip 在 MacOS 上因 SSLError 失败 - 如何恢复?

python - 无法更新 python 类中的属性

python - 导入错误 : cannot import name '_counter' from 'Crypto.Util'

python - 在 facebook 上发布,无需 api,但通过 http 请求

python - 如何使用 PropertyMock 在 pytest 单元测试中返回请求响应属性?

Python HTTPConnectionPool 无法建立新连接 : [Errno 11004] getaddrinfo failed

Python 社交验证 : How to define a different username scheme?