JSON-API for循环的多处理

2024-09-28 18:44:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现查询以下内容占用了大部分的加载时间,有什么方法可以多处理这个吗?列表和变量已经在前面定义过,并且包含数据

for i in range(len(allTrainDataList)):
      allTrainsRouteJSON = requests.get('https://api.com/'+mergeAllTrainDataList[i]+'/'+date,
                        auth=('username', 'password'))
      allTrainServiceJson = allTrainsRouteJSON.json()
      listOfAllStations=[]

      try: noSchedule = str(allTrainServiceJson['error'])
      except: noSchedule="FALSE"

      if noSchedule=="FALSE":

#Sanity check - Was main station on the planned route?
        for j in allTrainServiceJson['locations']:

  #Potentially put the if statements here to run each of the route defs    
            listOfAllStations.append(str(j['crs'])+mergeAllTrainDataList[i])
            toBeChecked=[fromStation+allTrainDataList[i],toStation+mergeAllTrainDataList[i]]
            toBeChecked2=[returnFromStation+allTrainDataList[i],returnToStation+mergeAllTrainDataList[i]]

            if contains_in_order(listOfAllStations,toBeChecked):
              allTrainDataListFinal.append(mergeAllTrainDataList[i])       
            if contains_in_order(listOfAllStations,toBeChecked2):
              allTrainDataListFinal2.append(mergeAllTrainDataList[i])

Tags: theinfalseforifrouteappendstr