GCP Cloud Funtions多线程发布请求

时间:2019-01-16 11:57:36

标签: python-3.x multithreading google-cloud-platform google-cloud-functions

所以我在下面有这段代码,它在x个项目中吐出一个ID列表,然后对其进行迭代,并对URL(谷歌云函数)进行多个request.post的访问,但是,它仅发送最后一个有效载荷。

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行的输出,您将看到有效载荷具有相同的内容。

谢谢

1 个答案:

答案 0 :(得分:1)

问题是您正在使用payload.update()来突变payload对象,然后将其传递。这导致每个线程都使用相同的有效负载,因为在发出请求时,原始payload已更新为具有最后一组_ids

相反,从现有有效负载中创建一个新的dict

for _ids in limits:
    t = Thread(target=worker, args=(recovery_url, dict(**payload, instances=_ids)))
    t.start()