python-3.x - GCP Cloud Functions 多线程发布请求
问题描述
所以我在下面有这段代码,它在 x 项中吐出一个 id 列表,然后遍历它们并request.post
生成一个 URL(谷歌云函数)的倍数,但是它只发送最后一个有效负载。
from threading import Thread
from pprint import pprint
import logging
import requests
import os
import time
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',)
def worker(url, payload):
response = requests.post(url, json=payload)
pprint(payload)
response.headers['Content-Type'] = "application/json"
response.raise_for_status()
pprint(response.content.decode('UTF-8'))
def chunks(l, n):
"""Yield successive n-sized chunks from a list."""
for i in range(0, len(l), n):
yield l[i:i + n]
secret = 'sometoken'
recovery_url = 'https://some.cloudfuntion.url.here'
ids = [2018345610079096363, 2977875406043034415, 3271712161922849730, 419748955619930226,
5244962103551406903, 5803235572026782321, 7879812282087191078, 9098437513491289540]
payload = {
"message": secret,
"action": "DR",
"project": 'planar-depth-225211'
}
# LIMIT DR REQUESTS BY A SPECIFIC INT
limits = list(chunks(ids, 4))
for _ids in limits:
payload.update({"instances": _ids})
t = Thread(target=worker, args=(recovery_url, payload,))
t.start()
输出:
(Thread-1 ) Starting new HTTPS connection (1): xxx.cloudfunctions.net:443
(Thread-2 ) Starting new HTTPS connection (1): xxx.cloudfunctions.net:443
(Thread-2 ) https:xxx.cloudfunctions.net:443 "POST /recovery HTTP/1.1" 200 21
'Recovery with Success'
'Recovery with Success'
(Thread-1 ) https://xxx.cloudfunctions.net:443 "POST /recovery HTTP/1.1" 200 21
我可以编写 request.post 1st 然后执行它们吗?我不明白为什么只发布最后生成的有效载荷。如果您检查第 13 行的输出,您将看到有效负载具有相同的内容。
谢谢
解决方案
问题是您正在使用payload.update()
对payload
对象进行变异,然后将其传递。这导致每个线程都使用相同的有效负载,因为在发出请求时,原始负载payload
已更新为具有最后一组_ids
.
dict
相反,从现有有效负载创建一个新的:
for _ids in limits:
t = Thread(target=worker, args=(recovery_url, dict(**payload, instances=_ids)))
t.start()
推荐阅读
- jax-rs - 具有外部接口的 Quarkus Jax-rs 客户端
- python - 在 Python 中加载多个 json 文件,并在这些文件中检查键 'description' 的值是否为字符串且非空
- android - 切换到夜间模式时我的应用程序崩溃
- javascript - 如何在 Express.js 中将 SSR 添加到现有的 React.js 应用程序
- .net - 如何操作 NuGet 包的依赖关系?
- c# - Json 反序列化在运行测试时似乎立即生效,但在调试测试时需要很长时间
- python - 将烧瓶 wtform 的默认日期设置为今天
- python - 使用 pySmartDL 继续下载
- terraform - 有条件地运行 terraform 模块
- python - 从主机上的 python 脚本在 docker 容器中运行 python 命令