Python并发请求下限制QPS(每秒查询率)的实现代码
(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )
"htmlcode">
import grequests urls = [ "https://www.baidu.com", "https://www.google.com" ] requests = [ grequests.get(url) for url in urls ] * 1000 rate = 20 # 表示 20 请求/秒
time.sleep(1)
"normal">请
from time import sleep req_groups = [ requests[i: i+rate] for i in range(0, len(requests), rate) ] ret = [] for req_group in req_groups: ret += grequests.map(req_group) sleep(1) print(ret)
令牌桶(token bucket)方法
"normal">请
from time import time class Throttle: def __init__(self, rate): self.rate = rate self.tokens = 0 self.last = 0 def consume(self, amount=1): now = time() if self.last == 0: self.last = now elapsed = now - self.last if int(elapsed * self.rate): self.tokens += int(elapsed * self.rate) self.last = now self.tokens = ( self.rate if self.tokens > self.rate else self.tokens ) if self.tokens >= amount: self.tokens -= amount else: amount = 0 return amount throttle = Throttle(rate) req_groups = [ requests[i: i+rate] for i in range(0, len(requests), rate) ] ret = [] for req_group in req_groups: ret += grequests.map(req_group) while throttle.consume(): pass # 阻塞 print(ret)
GRequests-Throttle
"htmlcode">
pip install grequests-throttle
import grequests_throttle as gt ret = gt.map(requests, rate=rate) print(ret)
总结
如果并发请求数量较小,可以考虑使用time.sleep(1)简单快捷;当并发请求数量较大时,使用令牌桶(token bucket)方法能最大化利用每一秒;如果不想写太多代码,可以使用GRequests-Throttle包进行请求流量控制。
下一篇:python3读取autocad图形文件.py实例