【发布时间】:2021-09-22 13:13:46
【问题描述】:
目前,我正在尝试改进向网页发送多个 HTTP 请求的代码,直到它可以捕获一些文本(代码通过已知模式本地化)或直到 180 秒用完(我们等待页面给我们一个答案)。 这是代码的一部分(出于隐私目的稍作编辑):
if matches == None:
txt = "No answer til now"
print(txt)
Solution = False
start = time.time()
interval = 0
while interval < 180:
response = requests.get("page address")
subject = response.text
matches = re.search(pattern, subject, re.IGNORECASE)
if matches != None:
Solution =matches.group(1)
time = "{:.2f}".format(time.time()-start)
txt = "Found an anwswer "+ Solution + "time needed : "+ time
print(txt)
break
interval = time.time()-start
else:
Solution = matches.group(1)
它运行正常,但有人告诉我,执行“循环中的无限请求”可能会导致服务器的 CPU 使用率很高。你们知道我可以使用什么来避免这种情况吗?
Ps:我听说在 PHP 中人们使用 curl_multi_select() 来处理这些事情。不知道我说的对不对。
【问题讨论】:
标签: python-3.x python-requests httprequest cpu-usage