为什么这两个简单的Python多线程代码之间有很大的性能差异?

问题描述 投票:0回答:1

让我们考虑一下这个Python代码:

def process_payload(payload, url, headers):
    response = requests.post(url, headers=headers, json=payload)
    return response

def parallel_group2(payloads, url, headers):
    with ThreadPoolExecutor() as executor:
        results = executor.map(process_payload,payloads, [url]*len(payloads), [headers]*len(payloads))
    return list(results)

def parallel_group(payloads, url, headers):
    with ThreadPoolExecutor() as executor:
        results = executor.map(requests.post, [url]*len(payloads), [headers]*len(payloads), payloads)
    return list(results)

times = []
# payloads grouped by 15
payloads_grouped = [payloads[i:i+15] for i in range(0, len(payloads), 15)]
print( "shape of payloads_grouped", len(payloads_grouped), " x ", len(payloads_grouped[0]))
for i in range(3):
    start = time.time()
    with ThreadPoolExecutor() as executor:
        # results = executor.map(parallel_group2, payloads_grouped, [url]*len(payloads_grouped), [headers]*len(payloads_grouped))
        results = executor.map(parallel_group, payloads_grouped, [url]*len(payloads_grouped), [headers]*len(payloads_grouped))
    end = time.time()
    times.append(end-start)
    print( "Durations of iterations:", times)
print( "Durations of iterations:", times)
print( "Average time for 150 requests:", sum(times)/len(times))

当我使用并行组运行脚本时,我得到的结果非常一致:

Durations of iterations: [5.246389389038086, 5.195073127746582, 5.278628587722778]
Average time for 150 requests: 5.2400303681691485

当我使用 parallel_group2 运行它时,结果看起来更像这样:

Durations of iterations: [10.99542498588562, 9.43007493019104, 23.003321170806885]
Average time for 150 requests: 10.142940362294516

是否有人对 python 多线程有很好的了解,并且可以解释为什么对 request.post 的多线程调用和对只执行 requests.call 的函数的调用之间存在如此大的差异?我完全不明白。

我多次运行之前的代码,结果是一致的。

编辑: url 是 openai 的聊天完成 api ="api.openai.com/v1/chat/completions"

python multithreading asynchronous openai-api
1个回答
0
投票

你的函数parallel_group没有按照你希望的那样做。原因是您传递给 requests.post 的 3 个参数中,只有第一个参数(URL)是正确的。有效负载将被分配为data,标头将被分配为json API 最有可能返回错误,但你忽略了这种可能性

© www.soinside.com 2019 - 2024. All rights reserved.