我正在尝试创建一个脚本,它可以同时向一个页面发送超过 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/