【发布时间】:2021-12-29 08:32:31
【问题描述】:
我正在准备查询某些端点的代码。代码没问题,效果很好,但需要太多时间。我想使用 Python multiprocessing 模块来加快进程。我的主要目标是并行处理 12 个 API 查询。处理完作业后,我想获取结果并将它们放入字典列表中,一个响应作为列表中的一个字典。 API 响应采用 json 格式。我是 Python 新手,在这种情况下没有经验。
我想在下面并行运行的代码。
def api_query_process(cloud_type, api_name, cloud_account, resource_type):
url = "xxx"
payload = {
"limit": 0,
"query": f'config from cloud.resource where cloud.type = \'{cloud_type}\' AND api.name = \'{api_name}\' AND '
f'cloud.account = \'{cloud_account}\'',
"timeRange": {
"relativeTimeType": "BACKWARD",
"type": "relative",
"value": {
"amount": 0,
"unit": "minute"
}
},
"withResourceJson": True
}
headers = {
"content-type": "application/json; charset=UTF-8",
"x-redlock-auth": api_token_input
}
response = requests.request("POST", url, json=payload, headers=headers)
result = response.json()
resource_count = len(result["data"]["items"])
if resource_count:
dictionary = dictionary_create(cloud_type, cloud_account, resource_type, resource_count)
property_list_summary.append(dictionary)
else:
dictionary = dictionary_create(cloud_type, cloud_account, resource_type, 0)
property_list_summary.append(dictionary)
【问题讨论】:
标签: python python-multiprocessing python-multithreading