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,但它没有解决问题.任何帮助或建议将非常感谢.
似乎您的算法应该类似于以下内容:
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.总结
以上是内存溢出为你收集整理的c# – 对远程api的节流并行请求全部内容,希望文章能够帮你解决c# – 对远程api的节流并行请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)