【问题标题】:Faster request sending in python在 python 中发送更快的请求
【发布时间】:2021-12-19 19:19:01
【问题描述】:

我希望它每秒发送约 25 个请求。请帮忙。

import requests
x = 1

for i in range(100):
    requests.get("...")
    print(x)
    x += 1

【问题讨论】:

  • 也许看看异步或多处理#
  • 你是想发动 ddos​​ 攻击还是什么?
  • 不,我不是坏人
  • 你可以启动多个子流程或一起使用更多的程序
  • 我是初学者,多个子进程对我没有任何意义

标签: python


【解决方案1】:

您可以使用线程同时运行多个请求。查看this教程,了解更多关于python线程的信息。

在你的情况下,你可以使用这样的东西:

from threading import Thread

# target function
def foo():
    for i in range(4): 
        requests.get("...")

# create 25 new threads
threadList = []
for t in range(25):
    newThread = Thread(target=foo)
    threadList.append(newThread)

# start all threads
for t in threadList:
    t.start()

for t in threadList:
    t.join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2017-04-27
    • 2013-12-05
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    相关资源
    最近更新 更多