-4

We are implementing TPL (C#) in one of our applications where we are uploading files to server in parallel. When we try to upload 15 files in parallel with MaxdegreeOfParallism set to 4 only 12 files are uploaded and three files are not uploaded. We changed the MaxDegreeOfParallelism to 2 then 14 files are uploaded. I am not sure how does MaxDegreeOfParallelism work. Could any one clarify the use of MaxDegreeOfParallelism and what could be the reason of non processing the files.

My code

Parallel.ForEach(fileCollection,new ParallelOptions{ MaxDegreeOfParallelism = 2 },
            number => { fileNumber = UploadFile(file, response); });

Upload function will upload files to AmazonS3.

When we upload the files with MaxDegreeOfParallelism =1 then all files are uploaded properly whereas when we change the MaxDegreeOfParallelism to 2 only 14 files got uploaded out of 15 and if we change to 4 only 12 files got uploaded.

My Development system is quad core window 10.

Sarathy
  • 442
  • 2
  • 9
  • 20

1 Answers1

-2

Hard to know what you need help with unless you provide some code to put it in context, but in general, the MaxDegreeOfParallelism is summed up on the MSDN documentation.

My understanding is that it is used to limit the number of concurrent Tasks to something less than the default, which is the number of available threads. That may not necessarily correspond to the number of Web Requests which I presume is what you're concerned with if you're looking at file uploads.

https://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(v=vs.110).aspx

The MaxDegreeOfParallelism property affects the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance. A positive property value limits the number of concurrent operations to the set value. If it is -1, there is no limit on the number of concurrently running operations.

By default, For and ForEach will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

Chris Disley
  • 1,286
  • 17
  • 30