c# – 对远程api的节流并行请求

c# – 对远程api的节流并行请求,第1张

概述我正在使用一个使用Google Maps Geocoding API的ASP.NET MVC应用程序.在一个批处理中,最多可能有1000个查询要提交给Geocoding API,因此我尝试使用并行处理方法来提高性能.负责为每个核心启动进程的方法是: public void GeoCode(Queue<Job> qJobs, bool bolKeepTrying, bool bolSpellChec 我正在使用一个使用Google Maps GeoCoding API的ASP.NET MVC应用程序.在一个批处理中,最多可能有1000个查询要提交给GeoCoding 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,但它没有解决问题.任何帮助或建议将非常感谢.

解决方法 听起来你错过了某种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.
总结

以上是内存溢出为你收集整理的c# – 对远程api的节流并行请求全部内容,希望文章能够帮你解决c# – 对远程api的节流并行请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1227495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存