2010-07-14 36 views
4

我正在使用Google Maps Geocoding API的ASP.NET MVC应用程序。在一个批处理中,可能会有多达1000个查询提交给Geocoding API,因此我试图使用并行处理方法来提高性能。负责启动的过程针对每个核心的方法是:加油并行请求到远程api

public void GeoCode(Queue<Job> qJobs, bool bolKeepTrying, bool bolSpellCheck, Action<Job, bool, bool> aWorker) 
    { 
     // Get the number of processors, initialize the number of remaining 
     // threads, and set the starting point for the iteration. 
     int intCoreCount = Environment.ProcessorCount; 
     int intRemainingWorkItems = intCoreCount; 

     using(ManualResetEvent mreController = new ManualResetEvent(false)) 
     { 
      // Create each of the work items. 
      for(int i = 0; i < intCoreCount; i++) 
      { 
       ThreadPool.QueueUserWorkItem(delegate 
       { 
        Job jCurrent = null; 

        while(qJobs.Count > 0) 
        { 
         lock(qJobs) 
         { 
          if(qJobs.Count > 0) 
          { 
           jCurrent = qJobs.Dequeue(); 
          } 
          else 
          { 
           if(jCurrent != null) 
           { 
            jCurrent = null; 
           } 
          } 
         } 

         aWorker(jCurrent, bolKeepTrying, bolSpellCheck); 
        } 

        if(Interlocked.Decrement(ref intRemainingWorkItems) == 0) 
        { 
         mreController.Set(); 
        } 
       }); 
      } 

      // Wait for all threads to complete. 
      mreController.WaitOne(); 
     } 
    } 

这是基于图案文件我上Microsoft's parallel computing web site找到。 问题是,Google API有10 QPS(企业客户)的限制 - 我正在打 - 然后我得到HTTP 403错误。这是我可以从并行处理中获益的一种方式,但限制了我所做的请求?我试过使用Thread.Sleep,但它不能解决问题。任何帮助或建议将非常感激。

回答

1

这听起来像你缺少某种Max in Flight参数。在队列中有工作时,而不是仅仅循环,您需要根据工作完成来限制您的提交。

好像你的算法应该是这样的:

submit N jobs (where N is your max in flight) 

Wait for a job to complete, and if queue is not empty, submit next job. 
+0

感谢您的答复 - 这让我思考的问题是不同的。 – markpirvine 2010-07-15 16:11:22