【发布时间】:2019-01-16 11:57:36
【问题描述】:
所以我在下面有这段代码,它在 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 行的输出,您将看到有效负载具有相同的内容。
谢谢
【问题讨论】:
标签: python-3.x multithreading google-cloud-platform google-cloud-functions