-2

I want to run a function asynchronously in parallel for all the objects in a collection. Here's the current code snippet.

Parallel.ForEach(Objs,
                new ParallelOptions { MaxDegreeOfParallelism = 10 },
                item =>
            {
                DoSomething(someParameter, item);
            });

How can I achieve this is with Async-Parallel Threads

Bee
  • 1
  • Check this: http://stackoverflow.com/questions/11564506/nesting-await-in-parallell-foreach – BytesOfMetal Oct 26 '16 at 10:26
  • Can you give an example of what you want to achieve? Do you want the `Parallel.ForEach`to run async? – nvoigt Oct 26 '16 at 10:27
  • @nvoigt Yes I want to run Parallel.ForEach run Async. – Bee Oct 26 '16 at 10:27
  • parallel by its means is already asnchronous... instead of processing with `for (synchronous (one after another))` it is run in parallel => (asynchronous (one parallel x others)) – Felix D. Oct 26 '16 at 10:34
  • @Motivated The `Parallel` class does not provide asynchronous methods, no. It does work in parallel, but it synchronously waits for all of the work being done in parallel to complete before returning, unlike what an asynchronous operation that did work in parallel would do. – Servy Oct 26 '16 at 14:58
  • @Servy yeah thats what I meant. still think the question is more about parallel processing rather then a real asynchronous operation – Felix D. Oct 26 '16 at 18:00
  • @Motivated Given that the quesiton shows a synchronous parallel operation and asks how to make it asynchronous, what makes you think he actually wants a synchronous parallel operation instead of an asynchronous parallel operation? If that was actually what he wanted, then his existing solution would work. Since he is specifically saying he wants this to be asynchronous instead, I see no reason to doubt that that's what he's asking how to do. – Servy Oct 26 '16 at 18:06

1 Answers1

1

It's not clear what you mean by "asynchronously in parallel," since asynchrony (using fewer threads) is kind of the opposite of parallel (using more threads).

But if you mean that you want to run the asynchronous method DoSomething concurrently, then that can be done via Task.WhenAll:

var tasks = Objs.Select(item => DoSomething(someParamter, item));
await Task.WhenAll(tasks);

If you want to limit the degree of concurrency (e.g., to 10), then you can use a SemaphoreSlim:

var semaphore = new SemaphoreSlim(10);
var tasks = Objs.Select(item =>
{
  await semaphore.WaitAsync();
  try { await DoSomething(someParameter, item); }
  finally { semaphore.Release(); }
});
await Task.WhenAll(tasks);
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • asynchrony does *not* mean using fewer threads. It just means that the work is done without blocking the thread that initiates the work. Whether it does or does not use threads to accomplish that is irrelevant. LIkewise, doing work in parallel does not require the use of multiple threads. Doing work in parallel simply means performing multiple productive tasks at the same time. That can be accomplished with multiple threads, or through other means. It is a synonym of doing work concurrently. – Servy Oct 26 '16 at 14:06
  • @Servy: We'll have to agree to disagree. I believe there is a definite need to distinguish concurrency from parallelism (or whatever terms you choose to use). – Stephen Cleary Oct 26 '16 at 14:52
  • You can distinguish concurrency or parallelism in general from multithreading, which is writing code that uses multiple threads. Just using words in ways that are completely different from their definitions, and worse, attempting to correct people *using words correctly based on their accepted definitions* to use your personal definition that nobody else shares is a horrible idea. If you think the distinction between parallel code that uses multiple threads and parallel code that doesn't use multiple threads is important than *use the correct terms* when describing those processes. – Servy Oct 26 '16 at 14:56
  • @Servy: I am using the correct, commonly-accepted definitions of my terms. The problem is that "parallel", "asynchronous", etc. have more than one definition, depending on who you talk to. – Stephen Cleary Oct 26 '16 at 15:30
  • Care to provide a reference source that "Parallel" work *only* refers to work done using multiple threads, and not work done in parallel without using multiple threads? – Servy Oct 26 '16 at 15:32
  • @Servy: [here you go](http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference). – Stephen Cleary Oct 26 '16 at 15:33
  • That defines it as two things being done at the same time *for example* by using multiple threads. That doesn't mean multiple threads are the only way to accomplish parallelism, it just means it's *one way* of doing so. There are other ways to do work at the same time that don't involve using multiple threads. For example, if you send out two network requests and then process the results when both complete, work is being done *in parallel*, even though no threads are (necessarily) being used. – Servy Oct 26 '16 at 15:37
  • @Servy: OK, let me turn it around: what terminology do *you* recommend? – Stephen Cleary Oct 26 '16 at 15:38
  • Asynchronously would mean that the operation starts and then continues to perform work after you execute the method. Parallelism means performing multiple tasks at the same time, which may or may not be done through the use of multiple threads. A program that uses multiple threads is a multithreaded program. So "asynchronously in parallel" means that the operation returns before it has finished, and the operation itself performs multiple component operations at the same time. It may be multithreaded to do that, it may not be. (Did I forget to define any relevant term?) – Servy Oct 26 '16 at 15:52
  • @Servy: I think tighter definitions would be more beneficial going forward. In particular, distinguishing between "asynchrony" and "multithreading". Also, using "parallel" as a synonym for "concurrent" (which it *is* in common usage, I concede) confuses people into using `Parallel` when they don't want multithreading. – Stephen Cleary Oct 26 '16 at 16:05