Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are these two code snippets using IQueryable and .AsParallel equivalent?

I am working on some pretty basic TPL code, and I ran across a situation where I was curious if the following two snippets are equivalent:

myEnumerable.AsParallel().Select(e =>
{
    //do some work that takes awhile
    return new Thing(e);
}

myEnumerable.Select(e =>
{
    //do some work that takes awhile
    return new Thing(e);
}.AsParallel()

Also - if they are, in fact, equivalent, is their equivalency something that can change as defined by the TPL interface with IEnumerable extension methods? Or am I just setting myself up to break my code when I update to .NET V{Whatever}?

For background, myEnumerable is an EF table (entity) that I have not yet enumerated (made the DB round trip) on.

My desired behavior is for the DB call to be made synchronously, get a List back, and operate upon the list in parallel (make a bunch of web service calls on the List in parallel)

like image 408
Codeman Avatar asked Dec 25 '22 20:12

Codeman


1 Answers

I was curious if the following two snippets are equivalent

No, they aren't. Your former code will attempt to partition the IEnumerable in order to execute it in parallel. You latter code will project elements to your Select sequentially, and receive the filtered IEnumerable. Only what comes after the AsParallel will run in parallel.

Note that LINQ-To-Entities doesn't really work with AsParallel. Usually, it will cause your code to run slower then it will sequentially. Also, DbContext is not thread-safe. That code will potentially cause more harm then good.

What you can do is first query the database, and once the data is in-memory, use AsParallel.

My desired behavior is for the DB call to be made synchronously, get a List back, and operate upon the list in parallel (make a bunch of web service calls on the List in parallel)

If you want to make multiple web service calls via the returned data, you can take advantage of the natural async API that exists for making such requests. For example, if you're querying an HTTP endpoint, you can exploit HttpClient and use it in combination with async-await, and execute queries concurrently, without needing any extra threads.

like image 124
Yuval Itzchakov Avatar answered Dec 29 '22 06:12

Yuval Itzchakov