期待从Parallel.ForEach循环中收益率返回 [英] Looking to Yield Return from a Parallel.ForEach loop

查看:82
本文介绍了期待从Parallel.ForEach循环中收益率返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多关于收益率回报和parallel.foreach的问题,但没有一个人有这么好的理由。 我相信我找到了一个案例,并且对它是否可以解决而感兴趣。

I've seen a number of questions regarding yield return and parallel.foreach, but none had a really good reason for doing it.  I believe I have found a case, and would be really interested whether or not it's solvable.

我有n个Xml文件。 每个包含n个节点,我将转换为IPaper对象。 这些对象将以y个记录块的形式推送到SQL服务器数据库。

I have n Xml files.  Each one contains n nodes that I will convert to IPaper objects.  These objects will be pushed to a SQL server database in blocks of y records.

我无法以列表形式读取它们的原因< IPaper>并且只返回yield返回列表中的每个元素,每个XML文件大小约为1.2 GB,我将使用XmlReader读取它们,以避免将整个内容加载到内存中。

The reason I can't read them in as a List<IPaper> and just yield return each element in the list is that each XML file is about 1.2 GB in size and I'll be reading through them using an XmlReader to avoid loading the whole thing into memory.

我意识到我可以通过一次处理一个文件来解决这个问题,但文件位于不同的网络驱动器上,所以我不会为磁盘IO而战,我有16个处理器和128 GB的RAM。 尽可能一次性地完成最多工作
似乎是明智的,我不在乎他们的订单是什么。

I realize I can get around this problem by processing each file one at a time, but the files are on separate network drives so I won't be fighting for disk IO, I have 16 processors and 128 GB of RAM.  Just seems sensible to do the most work at once as possible and I don't care what order they come in.

基本上只是懒惰,宁愿避免自定义线程选项&NBSP;思考Parallel.ForEach可能有快速简便的方法。

Basically just being lazy, prefer to avoid a custom threading option.  Thought Parallel.ForEach might have quick and easy way to do it.

谢谢!

推荐答案

Parallel.ForEach对集合进行操作但不返回集合,因此"yield"返回"并不是真的有用。 听起来你对PLINQ更感兴趣,它允许你编写并行化的LINQ查询,例如:列表与LT; IPaper> paper = files.AsParallel()。SelectMany(f
=> ReadPapers(f))。ToList()或类似的东西。

Parallel.ForEach operates on a collection but doesn't return a collection, so "yield return" isn't really relevant.  It sounds like you're more interested in PLINQ, which lets you write parallelized LINQ queries, e.g. List<IPaper> paper = files.AsParallel().SelectMany(f => ReadPapers(f)).ToList(), or something like that.


这篇关于期待从Parallel.ForEach循环中收益率返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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