首页 > 解决方案 > 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 行的输出,您将看到有效负载具有相同的内容。

谢谢

标签: python-3.xmultithreadinggoogle-cloud-platformgoogle-cloud-functions

解决方案


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

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

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

推荐阅读