downloadStart = datetime.now()
while (True):
requestURL = transactionAPI.format(page = tempPage,limit = 5000)
response = requests.get(requestURL,headers=headers)
json_data = json.loads(response.content)
tempMomosTransactionHistory.extend(json_data["list"])
if(datetime.fromtimestamp(json_data["list"][-1]["crtime"]) < datetime(datetime.today().year,datetime.today().month,datetime.today().day - dateRange)):
break
tempPage += 1
downloadEnd = datetime.now()
有什么建議嗎threading或者類似的?
Outputs here
downloadtime 0:00:02.056010
downloadtime 0:00:05.680806
downloadtime 0:00:05.447945
您需要從兩個方面進行改進。
#1通過查看您的代碼,我可以看到一個改進,即創建datetime.today對象,而不是執行3次。檢查其他方法,如transactionAPI進一步優化。
#2:如果您使用多核CPU機器,那么您可以通過每頁跨越thread來利用機器。請參閱上面的修改代碼。