我有一個列表我想運行一個代碼塊,但列表長度爲幾千項,並且它需要太長的時間迭代它們。如何在python中同時在列表中的所有項目上運行代碼塊
有沒有一種方法可以讓我的腳本同時對該列表中的每個項目執行檢查?
我的代碼如下所示:
def check_url(url_loc):
response1 = requests.get(url_loc)
time.sleep(10)
response2 = requests.get(url_loc)
if response1 != response2:
print(url_loc + "has changed go check now:" + url_loc)
else:
pass
while True:
all(check_url(loc) for loc in sitelocations)
爲什麼你有一個'time.sleep'在你的功能?這會減慢速度。這個功能應該做什麼? – qxz
@qxz這段代碼似乎比較相隔10秒,因此睡眠。 – bli