从BlockingCollection消费时不能使用AsOrdered< T> ? [英] AsOrdered cannot be used when consuming from BlockingCollection<T> ?

查看:102
本文介绍了从BlockingCollection消费时不能使用AsOrdered< T> ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据源为BlockingCollection时,似乎无法使用PLINQ查询中使用AsOrdered。我在阅读使用BlockingCollection时的问题< T>在Stephen Toub的文章中,我使用了他的GetConsumingPartitioner。虽然没有使用AsOrdered,但它的工作正常但是
(没有这个分区的BTW我已经解决了类似于人们在评论中提到的类似问题的文章)。当使用运算符AsOrdered时,评估开始但是从来没有结果 - foreach中的代码是
从未执行过,调用线程永远被阻塞。


我的代码看起来像这样: / p>

BlockingCollection< WorkItem> workQueue = new BlockingCollection< WorkItem>();


// ....


var dataItems = workQueue.GetConsumingPartitioner()。AsParallel() .AsOrdered()。选择(wi => LongTimeProcessing(wi));
$
foreach(dataItems中的var dataItem)

{

  ; outputStream.Write(dataItem,0,dataItem.Length);

}


因此可以创建一种可以与BlockingCollection一起使用的分区器AsOrdered?

解决方案

一些更正和澄清:


我发现了PLINQ查询上面写的抛出InvalidOperationException,消息"AsOrdered可能不会与不可订购的分区程序一起使用。"在Task中引发了异常,因此它被"隐藏"了。直到任务被"消耗",由于在Task中的代码(由于异常而未运行)和主线程(创建者和消费者)上的代码之间的某些依赖性(不包括在上面的代码示例中)而发生
任务)。我的错,实际上。


但我的问题仍然存在:是否可以编写可与BlockingCollection一起使用的可订购分区程序?


It seems that using AsOrdered in PLINQ query cannot be used when data source is BlockingCollection. I've read about problems when consuming BlockingCollection<T> in Stephen Toub's article so I've used his GetConsumingPartitioner. It works fine but only when AsOrdered is not used (BTW without this partitioner I've expirienced similar problems as people reported in comments at mentioned article). When operator AsOrdered is used the evaluation starts but there are never results - code in foreach is never executed and calling thread is blocked forever.

My code looks something like this:

BlockingCollection<WorkItem> workQueue = new BlockingCollection<WorkItem>();

// ....

var dataItems = workQueue.GetConsumingPartitioner().AsParallel().AsOrdered().Select(wi => LongTimeProcessing(wi));
foreach (var dataItem in dataItems)
{
 outputStream.Write(dataItem, 0, dataItem.Length);
}

So is it possible to create some kind of Partitioner which would work with BlockingCollection AND AsOrdered ?

解决方案

Some corrections and clarifications:

I've found out that PLINQ query written above throws InvalidOperationException with message "AsOrdered may not be used with a partitioner that is not orderable." The exception was raised in Task so it was "hidden" until Task would be "consumed", which never happened due to some dependencies (not included in above code sample) between code in Task (which was not run due to exception) and code on main thread (creator and consumer of task). My fault, actually.

But my question remains: is it possible to write orderable partitioner which would work with BlockingCollection ?


这篇关于从BlockingCollection消费时不能使用AsOrdered&lt; T&gt; ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆