针对循环的JSON API进行多处理

时间:2018-05-01 16:23:40

标签: python multiprocessing

我发现查询以下内容占用了大部分加载时间,有没有办法对此进行多处理?列表和变量之前已经定义并包含数据

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])

0 个答案:

没有答案