并行化循环调用OpenTripPlanner

时间:2016-11-20 23:21:39

标签: python multithreading for-loop parallel-processing opentripplanner

我有一个python脚本,它调用OpenTripPlanner来计算成对的地理定位点之间的旅行时间矩阵。 python脚本使用for loop计算每个出发时间的一个矩阵,并将其另存为单独的.csv文件。

我想将此操作并行化以使其更快。虽然这项任务似乎令人尴尬地平行,但我几乎不知道Python。根据我的阅读,我的预感是,最好的解决方案是使用multithreading,将每个spt保留在其中。

我真的很感激一些帮助。可以使用简单的可重现示例here

这是python脚本:

#!/usr/bin/jython
from org.opentripplanner.scripting.api import OtpsEntryPoint


# Instantiate an OtpsEntryPoint
otp = OtpsEntryPoint.fromArgs(['--graphs', '.',
                               '--router', 'sto'])

# Start timing the code
import time
start_time = time.time()

# Get the default router
router = otp.getRouter('sto')


# Read Points of Destination - The file points.csv contains the columns GEOID, X and Y.
points = otp.loadCSVPopulation('centroids_sto.csv', 'Y', 'X')
dests = otp.loadCSVPopulation('centroids_sto.csv', 'Y', 'X')


for h in range(7, 19):
  for m in range(0,60,30):

    # Create a default request for a given time
    req = otp.createRequest()
    req.setDateTime(2015, 12, 28, h, m, 00)
    req.setMaxTimeSec(3600) # 1h = 3600 seconds , 2h = 7200 seconds
    req.setModes('WALK,TRANSIT,BUS,TRAM,RAIL,SUBWAY')  # ("TRAM,RAIL,SUBWAY,FUNICULAR,GONDOLA,CABLE_CAR,BUS")


    # Create a CSV output
    matrixCsv = otp.createCSVOutput()
    matrixCsv.setHeader([ 'mode', 'depart_time', 'origin', 'destination', 'walk_distance', 'travel_time' ]) # travel_time in seconds

    # Start Loop
    for origin in points:
      print "Processing origin: ", str(h)+"-"+str(m)," ", origin.getStringData('idhex')
      req.setOrigin(origin)
      spt = router.plan(req)
      if spt is None: continue

      # Evaluate the SPT for all points
      result = spt.eval(dests)

      # Add a new row of result in the CSV output
      for r in result:
        matrixCsv.addRow([ 'public transport', str(h) + ":" + str(m) + ":00", origin.getStringData('idhex'), r.getIndividual().getStringData('idhex'), r.getWalkDistance() , r.getTime()])

    # Save the result
    matrixCsv.save('traveltime_matrix_sto_pt_'+ str(h)+"-"+str(m) + '.csv')


# Stop timing the code
print("Elapsed time was %g seconds" % (time.time() - start_time))

0 个答案:

没有答案