File.Copy在Parallel.ForEach [英] File.Copy in Parallel.ForEach
问题描述
您好我试图创建目录,并在Parallel.ForEach ...
Hi I try create dir and copy file (pdf) in Parallel.ForEach...
我创建简单的例子:
private static void CreateFolderAndCopyFile(int index)
{
const string sourcePdfPath = "c:\\testdata\\test.pdf";
const string rootPath = "c:\\testdata";
string folderDirName = string.Format("Data{0}", string.Format("{0:00000000}", index));
string folderDirPath = rootPath + @"\" + folderDirName;
Directory.CreateDirectory(folderDirPath);
string desPdfPath = folderDirPath + @"\" + "test.pdf";
File.Copy(sourcePdfPath, desPdfPath, true);
}
上面的方法创建新的文件夹和PDF文件复制到新的文件夹。
它创建这个目录树:
Method above create new folder and copy pdf file to new folder. It creates this dir tree:
TESTDATA
-Data00000000
-test.pdf
-Data00000001
-test.pdf
....
-Data0000000N
-test.pdf
我称之为Parallel.ForEach环法CreateFolderAndCopyFile。
I call method CreateFolderAndCopyFile in Parallel.ForEach loop.
private static void Func<T>(IEnumerable<T> docs)
{
int index = 0;
Parallel.ForEach(docs, doc =>
{
CreateFolderAndCopyFile(index);
index++;
});
}
如果我尝试运行这段代码是错误结束:
If I try run this code it finish with error:
该进程无法访问该文件'C:\testdata\Data00001102\test.pdf'
,因为它正由另一个进程使用
The process cannot access the file 'c:\testdata\Data00001102\test.pdf' because it is being used by another process.
但首先它创造了1111年新的文件夹,并复制大约1111倍的test.pdf和我得到这个错误。
But first it created 1111 new folders and copy test.pdf about 1111 times and the I got this error.
什么原因此行为以及如何解决它。
What cause this behaviour and how to solve it?
编辑:
以上代码是玩具样品,比较遗憾的硬编码字符串
结论:并行方法SI慢
Code above was toy sample, sorry for hard coded strings Conclusion: Parallel method si slow.
明天我试着从一些方法如何在C#写超级快速文件流的代码?。
特别:的 http://designingefficientsoftware.wordpress.com/2011/03/03/efficient-file-io-from-csharp/
推荐答案
您没有进行同步访问首页
,这意味着你有就可以了比赛。这就是为什么你有错误。为了便于说明,可以避开种族和使用 Interlocked.Increment
。
You are not synchronizing access to index
and that means you have a race on it. That's why you have the error. For illustrative purposes, you can avoid the race and keep this particular design by using Interlocked.Increment
.
private static void Func<T>(IEnumerable<T> docs)
{
int index = -1;
Parallel.ForEach(
docs, doc =>
{
int nextIndex = Interlocked.Increment(index);
CreateFolderAndCopyFile(nextIndex);
}
);
}
然而,正如其他人建议,的 的ForEach
,提供了一个循环索引显然是一个更清洁的解决了这个特别的问题。
However, as others suggest, the alternative overload of ForEach
that provides a loop index is clearly a cleaner solution to this particular problem.
但是,当你得到它的工作,你会发现,复制文件IO约束,而不是必然的处理器和我预测的并行代码会比串行慢代码。
But when you get it working you will find that copying files is IO bound rather than processor bound and I predict that the parallel code will be slower than the serial code.
这篇关于File.Copy在Parallel.ForEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!