PLINQ AsParallel() 优先级较低?
是否可以运行某些 PLINQ AsParallel() - 优先级低于其他查询的查询? (或者某些优先级高于其他) 这可以通过 PLinq 实现吗?还是我必须避免使用 PLINQ 并自己完成所有工作?
编辑/更新:
是否可以在并行执行的方法内部调用
Thread.Sleep(0)
当我想归档较低优先级时, ? 或者这是一个非常糟糕的做法/黑客?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,这在 PLINQ 中不能直接实现。
您可以通过创建自定义 任务计划程序。这将允许您在使用 Parallel.For 或 ForEach 时拥有自定义“优先级”。
然而,PLINQ 并未提供自定义 TaskScheduler 的功能,因为 PLINQ 需要 TaskScheduler 提供非常严格的保证,而且担心暴露这一点会带来很大问题。
这会“降低”优先级,但不幸的是,它有自己的问题,特别是与 PLINQ 结合使用时。这可能会导致 ThreadPool 中的线程匮乏,因为您将在 ThreadPool 线程上“休眠”。
此外,这还存在一个根本问题 - PLINQ 是为处理查询而设计的,而不是为处理而设计的。引入逻辑代码来控制流结构确实违背了 PLINQ 背后的理论,并且可能会导致您意想不到的性能影响,尤其是在使用默认分区时。
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
orForEach
.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.
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.
AsParallel
是非常高级的 API。如果您想使用 优先级AsParallel
is very high level API. You should really useThread
s if you want fine grained control over what is happening using PriorityPLINQ 库不允许配置
TaskScheduler
,有充分的理由:如果您喜欢冒险,可以考虑通过反射进行此配置。下面是一个完全执行此操作的 PLINQ 运算符:
使用示例:
您可以使用相同的
lowPriorityScheduler
来调度应以低优先级运行的其他操作,与此查询和其他查询并行运行。所有这些操作组合最多将使用Environment.ProcessorCount
来自的线程线程池
。超出此限制的所有其他可用线程将专门由非低优先级操作使用。您还可以查看 这个有点相关的问题。
The PLINQ library does not allow configuring the
TaskScheduler
, for good reasons:If you feel adventurous you could consider doing this configuration with reflection. Here is a PLINQ operator that does exactly this:
Usage example:
You could use the same
lowPriorityScheduler
to schedule other operations that should run with low priority, in parallel with this and other queries. All these operations combined will use at mostEnvironment.ProcessorCount
threads from theThreadPool
. All other threads that are available beyond this limit, will be used exclusively by non-low-priority operations.You could also take a look at this somewhat related question.