简体   繁体   English

Parallel.ForEach中的二进制序列化/反序列化(BinaryFormatter)

[英]binary serialization/deserialization (BinaryFormatter) in Parallel.ForEach

Perhaps this is a duplication(( But I have not found any sample code of the using of binnaryformatter in Parallel.Foreach. Could anyone provide a sample of it? Sample of code 也许这是重复的((但是我在Parallel.Foreach中没有找到使用binnaryformatter的任何示例代码。有人可以提供它的示例吗?

Parralel.ForEach(files, fileCurr=>
                  {
                      using(lib.Accesser("fileType", fileNameSpec))
                      {
                        LoadFileData(fileNameSpec,fileCurr,cancellationToken, progressCallback);
                      }
                   });
and

LoadFileData(fileNameSpec,fileCurr,cancellationToken, progressCallback)
{
 using(lib2.load(fileCurr.name))
{
 foreach(var v in fileCurr.include)
 {
   var objectForSerialization = loadObj(v);
   //my code goes below
   System.IO.Stream stream = System.IO.File.Open("J:\\volume", FileMode.OpenOrCreate);
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter = new BinaryFormatter();
                    bformatter.Serialize(stream, objectForSerialization);
                    stream.Close();
  //but this fil will be locked by multyply writes

 }
}
}

One part of your problem is that 问题的一部分是

reading and writing or executing multiple writes in the same file in parallel is not realistically possible. 实际上不可能并行读取和写入或在同一文件中执行多次写入。

Once you implement a locking mechanism for the file reads / writes, there is no longer any need to execute your iteration in parallel because its operations cannot be executed in parallel . 一旦为文件读写实现了锁定机制,就不再需要并行执行迭代,因为它的操作无法并行执行

If you are only deserialising , (just reading from the files) it's a different story. 如果您只是反序列化 (仅从文件中读取),则是另一回事了。

Read-only access can be done in parallel because it does not require locking of the file per sé. 只读访问可以并行完成,因为它不需要按秒锁定文件。

see : How can I read a text file without locking it? 参见: 如何在不锁定文本的情况下读取文本文件?

Another thing you could do, is preload the text files (if this is possible) and deserialise them straight from the resulting strings. 您可以做的另一件事是,预加载文本文件(如果可能的话),然后直接从生成的字符串中反序列化它们。

::edit:: ::编辑::

one more thing : 还有一件事 :

You appear to always be writing to the same file on your J: drive (a file called volume with no extention). 您似乎总是在写入J:驱动器上的同一文件(一个没有volume名为volume的文件)。

If you are serialising multiple objects, you will want them to be serialised to different files... 如果要序列化多个对象,则希望将它们序列化为不同的文件...

If you can assure yourself that all objects are serialised to different files, you can make the operation parallel. 如果可以确保将所有对象序列化到不同的文件,则可以使操作并行。

This would be someting like this: 这将是这样的:

 System.IO.Stream stream = System.IO.File.Open("J:\\volume\\" + GenerateFileName(v), FileMode.OpenOrCreate);

Then make sure GenerateFileName generates a unique file name as string. 然后确保GenerateFileName生成一个唯一的文件名作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM