'Can we have control over CPU usage when using Parallel.Foreach in C#?
I want to process thousands of records using Parallel.Foreach, but the CPU usage is 100%.
Parallel.ForEach(PhotoPaths, new ParallelOptions { MaxDegreeOfParallelism = 10 }, photopath =>
{
RemovePhoto(photo);
});
public void RemovePhoto(string photopath)
{
File.Delete(photopath);
}
If I use MaxDegreeOfParallelism = 2, my operation is getting timed out.
With a normal foreach loop it is getting timed out so I have choose Parallel.Foreach with which my task is completing but the only problem is CPU utilisation is 100%. Please let me know if we have any alternatives.
Solution 1:[1]
Unlike some ppl in the comments, I understand the point of your question. It's quite reasonable to want control over the amount of CPU a processes uses. Fastest / most CPU possible is not always desirable. For example, you might have a top level thread that needs to servicing requests in a responsive manner. Or you might want to use your computer while this process is running. So a much better option would be to use less than 100% of the CPU.
I run a distributed app at home and, depending on settings, it runs at 100% CPU on unattended PCs and at 50% on PCs that are in use...
So yeah, try MaxDegreeOfParallelism = Environment.ProcessorCount / 2
(by default MaxDegreeOfParallelism is set to Environment.ProcessorCount)
Also, and sorry about this, I personally dislike it when ppl start asking why this why that rather than just answering the question........ But when you say your loop times out, what is waiting for the files to be deleted? Can you just queue the delete requests and delete them outside the loop, on another thread/task, at your leisure, without making the caller wait?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
