Skip to main content

parallelization - Is there any sequence using OpenAppend and ParalleTable for continuing writing data?


After I post the question writing data without erasing exist data when quit kernel , I can successfully use OpenAppend to realize it. But if I work with ParallelTable and write in two different ways, one works good and another seems does not work.




If I write in the following way, it seems not work. When I quit Kernels or close and run the code again, all the exist data are replaced by new data.


not working Case:



currentdirectory = NotebookDirectory[];
foldername = "StoreFolder";
pSite = FileNameJoin[{currentdirectory, foldername}];
If[DirectoryQ[pSite] == False, folderpath = CreateDirectory[pSite],
folderpath = pSite];


kernelNumber = 2; (*number of Kernels in my computer *)
storePath = {};
For[Kernelnum = 1, Kernelnum <= kernelNumber, Kernelnum++,
AppendTo[storePath, FileNameJoin[{folderpath, "store" <> ToString[Kernelnum] <> ".txt"}]];
OpenAppend[storePath[[Kernelnum]]];
];

Steps = 10;
testlist = {0, 0, 0, 0};
ParallelTable[

WriteLine[storePath[[$KernelID]], ToString[testlist]];,{ii, 1, Steps}];

For[Kernelnum = 1, Kernelnum <= kernelNumber, Kernelnum++,
Close[storePath[[Kernelnum]]];
];


If I write in the following way, it seems work. When I quit Kernels or close and run the code again, all the exist data are not erased.


working Case:




currentdirectory = NotebookDirectory[];
foldername = "StoreFolder";
pSite = FileNameJoin[{currentdirectory, foldername}];
If[DirectoryQ[pSite] == False, folderpath = CreateDirectory[pSite],
folderpath = pSite];

kernelNumber = 2; (*number of Kernels in my computer *)
storePath = {};
For[Kernelnum = 1, Kernelnum <= kernelNumber, Kernelnum++,
AppendTo[storePath, FileNameJoin[{folderpath, "store" <> ToString[Kernelnum] <> ".txt"}]];

];

Steps = 10;
testlist = {0, 0, 0, 0};
ParallelTable[OpenAppend[storePath[[$KernelID]]];
WriteLine[storePath[[$
KernelID]], ToString[testlist]];
Close[storePath[[$KernelID]]]; nn = iiLists, {iiLists, 1, Steps}];

I don't know why one work and another do not work, or did I miss something? Thank you very much!




Comments