gpt4 book ai didi

python - 发送同时请求python(一次全部)

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:48 24 4
gpt4 key购买 nike

我正在尝试创建一个脚本,它可以同时向一个页面发送超过 1000 个请求。但是请求具有线程 (1000) 个线程的库。似乎在 1 秒内完成了前 50 个左右的请求,而其他 9950 个请求则花费了相当长的时间。我是这样测的。

def print_to_cmd(strinng):
queueLock.acquire()
print strinng
queueLock.release()

start = time.time()
resp = requests.get('http://test.net/', headers=header)
end = time.time()

print_to_cmd(str(end-start))

我认为请求库限制了它们的发送速度。

有人知道在 python 中同时发送请求的方法吗?我有一个上传 200mb 的 VPS,所以这不是与 python 或限制它的请求库有关的问题。他们都需要在 1 秒内相互点击网站。

感谢阅读,希望有人能提供帮助。

最佳答案

我通常发现最好的解决方案是使用像 tornado 这样的异步库。然而,我找到的最简单的解决方案是使用 ThreadPoolExecutor。


import requests
from concurrent.futures import ThreadPoolExecutor

def get_url(url):
return requests.get(url)
with ThreadPoolExecutor(max_workers=50) as pool:
print(list(pool.map(get_url,list_of_urls)))

关于python - 发送同时请求python(一次全部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com