我正在使用Parallel.ForEach以一次类似1000请求的批处理模式调用rest api服务。我的MaxDegreeOfParallelism设置为1000,但是似乎系统一次只能创建10-15个请求,而系统CPU利用率非常正常(15%)
var maxDegree = new ParallelOptions() { MaxDegreeOfParallelism = MaxDegreeOfParallelism };
Parallel.ForEach(parsedLines, maxDegree, obj =>
{
processIndividualRecord(obj);
var currentCount = Interlocked.Increment(ref paraaleCounter);
if (currentCount % 100 == 0)
{
logger.Debug("Reaming records to process is:" + (parsedLines.Count - currentCount));
}
});
有什么方法可以一次发出1000个请求,以避免在非阻塞模式下产生大量等待时间。
您的ThreadPool
线程不足。一种快速的解决方法是通过使用ThreadPool.SetMinThreads
方法来增加初始的工作人员池。
ThreadPool.SetMinThreads
一种更好的方法是异步调用REST API。这样,将消除在请求进行过程中对工作线程的需求。 ThreadPool.SetMinThreads(1000, 10);
库是协调异步过程的绝佳工具。您可以看到此库TPL Dataflow的用法示例。