Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PLINQ AsParallel() with lower priority?

Tags:

c#

.net

plinq

is it possible to run some of my PLINQ AsParallel() - Queries with a lower priority than others? (Or some with a higher priority than others) Is this possible with PLinq or will I have to avoid PLINQ and do all the stuff on my own?

EDIT/UPDATE:

Would it be possible to call

Thread.Sleep(0)

inside the parallel executed method when I want to archive a lower priority? Or is that a very bad practice/hack?

like image 287
Chris Avatar asked Mar 04 '11 18:03

Chris


2 Answers

Unfortunately, this is not directly possible in PLINQ.

You can do it in most of the rest of the Task Parallel Library via creation of a custom TaskScheduler. This would allow you to have custom "priorities" when using Parallel.For or ForEach.

However, the ability to customize the TaskScheduler was not provided with PLINQ, since PLINQ requires very strict guarantees from the TaskScheduler, and the fear was that exposing this would be very problematic.


EDIT/UPDATE:

Would it be possible to call

Thread.Sleep(0)

This would "lower" the priority, but unfortunately, has it's own issues, especially when combined with PLINQ. This will potentially cause thread starvation in the ThreadPool, since you'll be "sleeping" on ThreadPool threads.

In addition, there's a fundamental problem with this - PLINQ is designed and intended to handle queries, and is not designed for processing. Introducing logical code to control the flow structure is really against the theory behind PLINQ, and will likely cause unintended performance repercussions you aren't expecting, especially if you're using the default partitioning.

like image 94
Reed Copsey Avatar answered Nov 11 '22 10:11

Reed Copsey


AsParallel is very high level API. You should really use Threads if you want fine grained control over what is happening using Priority

like image 43
Andrey Avatar answered Nov 11 '22 09:11

Andrey