我应该使用线程池或任务并行库的IO绑定操作 [英] Should i use ThreadPools or Task Parallel Library for IO-bound operations
问题描述
在我的项目,这些项目还挺聚合器之一,我解析饲料,播客等从网络上。
In one of my projects that's kinda an aggregator, I parse feeds, podcasts and so from the web.
如果我使用顺序的方法,因为有大量的资源,它需要相当长的时间来处理所有的人(因为网络问题和类似的东西);
If I use sequential approach, given that a large number of resources, it takes quite a time to process all of them (because of network issues and similar stuff);
foreach(feed in feeds)
{
read_from_web(feed)
parse(feed)
}
所以,我想要实现并发性,不能决定我是否应该基本上使用线程池与工作线程处理或仅仅依靠第三方物流得到它排序。
So I want to implement concurrency and couldn't decide if I should basically use ThreadPools to process with worker threads or just rely on TPL to get it sorted.
肯定会完成这项工作对我的工作线程,我会得到什么我希望(在多核CPU环境下,其它内核也将采用同样)的线程池。
ThreadPools for sure will handle the job for me with worker threads and I'll get what I expect (and in multi-core CPU environments, the other cores will be also utilized also).
但我还是要考虑TPL过,因为它是推荐的方法,但我有点担心了。首先,我知道TPL使用线程池,但增加了决策的附加层。我最关注,凡单核环境是present的条件。如果我没看错TPL开始与一些工人线程在一开始等于可用的CPU核心数量。我怕做第三方物流产生类似的结果,为我的IO绑定的情况下顺序的方法。
But I still want to consider TPL too as it's recommend method but I'm a bit concerned about it. First of all I know that TPL uses ThreadPools but adds additional layer of decision making. I'm mostly concerned of the condition that where a single-core environment is present. If I'm not wrong TPL starts with a number worker-threads equal to number of available CPU-cores at the very beginning. I do fear of TPL producing similar results to sequential approach for my IO-bound case.
因此,对于IO绑定操作(在我的情况下,从网上阅读资源),是最好用的线程池和控制的事情,或者更好仅仅依靠第三方物流?可以TPL也可以在IO绑定方案中使用?
So for IO-bound operations (in my case reading resources from web), is it best to use ThreadPools and control the things, or better just rely on TPL? Can TPL also be used in IO-bound scenarios?
更新:我主要关注的是 - 一个单核CPU上的 环境将TPL只是表现得像顺序的方法还是会仍然提供并发?我已经阅读并行编程和Microsoft .NET 等等的书但找不到此一个确切的答案
Update: My main concern is that -- on a single-core CPU environment will TPL just behave like sequential approach or will it still offer concurrency? I'm already reading Parallel Programming with Microsoft .NET and so the book but couldn't find an exact answer for this.
请注意:这是我的previous问题重新措辞[<一个href=\"http://stackoverflow.com/questions/5203871/is-it-possible-to-use-thread-concurrency-and-parallelism-together\">Is它可以使用线程并发和并行在一起吗?]这是非常错误的措辞。
Note: this is a re-phrasing of my previous question [ Is it possible to use thread-concurrency and parallelism together? ] which was quite phrased wrong.
推荐答案
所以我决定,而不是写测试这一点,看看它实际的数据。
So i instead decided to write tests for this and see it on practical data.
测试联想
- ITR:迭代
- SEQ:顺序的方法。
- PrlEx:并行扩展 - Parallel.ForEach
- TPL:任务并行库
- TPool:线程池
测试结果
的单核CPU [Win7-32] - VMware下运行 - 的
Test Environment: 1 physical cpus, 1 cores, 1 logical cpus.
Will be parsing a total of 10 feeds.
________________________________________________________________________________
Itr. Seq. PrlEx TPL TPool
________________________________________________________________________________
#1 10.82s 04.05s 02.69s 02.60s
#2 07.48s 03.18s 03.17s 02.91s
#3 07.66s 03.21s 01.90s 01.68s
#4 07.43s 01.65s 01.70s 01.76s
#5 07.81s 02.20s 01.75s 01.71s
#6 07.67s 03.25s 01.97s 01.63s
#7 08.14s 01.77s 01.72s 02.66s
#8 08.04s 03.01s 02.03s 01.75s
#9 08.80s 01.71s 01.67s 01.75s
#10 10.19s 02.23s 01.62s 01.74s
________________________________________________________________________________
Avg. 08.40s 02.63s 02.02s 02.02s
________________________________________________________________________________
的单核CPU [WinXP中] - VMware下运行 - 的
Test Environment: 1 physical cpus, NotSupported cores, NotSupported logical cpus.
Will be parsing a total of 10 feeds.
________________________________________________________________________________
Itr. Seq. PrlEx TPL TPool
________________________________________________________________________________
#1 10.79s 04.05s 02.75s 02.13s
#2 07.53s 02.84s 02.08s 02.07s
#3 07.79s 03.74s 02.04s 02.07s
#4 08.28s 02.88s 02.73s 03.43s
#5 07.55s 02.59s 03.99s 03.19s
#6 07.50s 02.90s 02.83s 02.29s
#7 07.80s 04.32s 02.78s 02.67s
#8 07.65s 03.10s 02.07s 02.53s
#9 10.70s 02.61s 02.04s 02.10s
#10 08.98s 02.88s 02.09s 02.16s
________________________________________________________________________________
Avg. 08.46s 03.19s 02.54s 02.46s
________________________________________________________________________________
的双核CPU [Win7-64] 的
Test Environment: 1 physical cpus, 2 cores, 2 logical cpus.
Will be parsing a total of 10 feeds.
________________________________________________________________________________
Itr. Seq. PrlEx TPL TPool
________________________________________________________________________________
#1 07.09s 02.28s 02.64s 01.79s
#2 06.04s 02.53s 01.96s 01.94s
#3 05.84s 02.18s 02.08s 02.34s
#4 06.00s 01.43s 01.69s 01.43s
#5 05.74s 01.61s 01.36s 01.49s
#6 05.92s 01.59s 01.73s 01.50s
#7 06.09s 01.44s 02.14s 02.37s
#8 06.37s 01.34s 01.46s 01.36s
#9 06.57s 01.30s 01.58s 01.67s
#10 06.06s 01.95s 02.88s 01.62s
________________________________________________________________________________
Avg. 06.17s 01.76s 01.95s 01.75s
________________________________________________________________________________
的四核CPU [Win7-64] - 海兰prerThreading支持 - 的
Test Environment: 1 physical cpus, 4 cores, 8 logical cpus.
Will be parsing a total of 10 feeds.
________________________________________________________________________________
Itr. Seq. PrlEx TPL TPool
________________________________________________________________________________
#1 10.56s 02.03s 01.71s 01.69s
#2 07.42s 01.63s 01.71s 01.69s
#3 11.66s 01.69s 01.73s 01.61s
#4 07.52s 01.77s 01.63s 01.65s
#5 07.69s 02.32s 01.67s 01.62s
#6 07.31s 01.64s 01.53s 02.17s
#7 07.44s 02.56s 02.35s 02.31s
#8 08.36s 01.93s 01.73s 01.66s
#9 07.92s 02.15s 01.72s 01.65s
#10 07.60s 02.14s 01.68s 01.68s
________________________________________________________________________________
Avg. 08.35s 01.99s 01.75s 01.77s
________________________________________________________________________________
概述
- 您是否在单核环境或多核之一,并行扩展运行,TPL和线程池的相同的行为,并给出近似的结果
- 还是 TPL 的优点一样容易异常处理,取消支持,能够方便地返回任务结果。虽然并行扩展也是另一种可行的选择。
- Whether you run on a single-core environment or a multi-core one, Parallel Extensions, TPL and ThreadPool behaves the same and gives approximate results.
- Still TPL has advantages like easy exception handling, cancellation support and ability to easily return Task results. Though Parallel Extensions is also another viable alternative.
上运行测试自己的
您可以在这里下载源和自己动手运行。如果你能后的结果,我还会添加。
You can download the source here and run on-your-own. If you can post the results, i'll add them also.
更新:固定源链接
这篇关于我应该使用线程池或任务并行库的IO绑定操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!