2010-07-14 4 views
4

Google지도 지오 코딩 API를 사용하는 ASP.NET MVC 응용 프로그램에서 작업하고 있습니다. 단일 배치에서 Geocoding API에 제출할 쿼리가 1000 개까지있을 수 있으므로 성능을 향상시키는 병렬 처리 방식을 사용하려고합니다. 각 코어에 대한 프로세스를 시작 담당하는 방법은 다음과 같습니다원격 응용 프로그램에 스로틀 병렬 요청

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

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

관련 문제