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

标签 python python-requests simultaneous-calls

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

相关文章:

python - 安装 Python 和 Django - 在哪里?

Python请求不清理连接导致端口溢出?

python - 响应[412]使用requests python包访问该网页时,如何绕过?

google-apps-script - '异常: Too many simultaneous invocations: Spreadsheets' - Is it because I am opening spreadsheets in a loop?

Python 多个用户同时追加到同一个文件

python - 跨应用程序可用的全局变量

python - 在Python 3中写入没有行空间的CSV文件

python - KivyMD 按钮 : How increase its dimensions

python - 为什么我无法使用 Python "requests"库填写此表单?

javascript - 如何处理同时发生的 javascript xmlhttp 请求?