# How to read and write the same text file in Parallelization using ParallelTable? [closed]

I want to know how to write and read a same text file (for example, external.txt ) without conflicts in parallelization. The following is just a small idea, but the problem is there seems a conflicts in writing to the same text file -- ObtainedData sometimes are only part written in the external.txt.

The following is the code just for describe the idea.

......

ParallelTable[
data = Import["external.txt"];
ExternalMemory = ImportString[data, "List"];

(*get data from some operations, here i ignore the codes using ...... *)
.....
.....

(*check whether the ObtainedData is in the external file, if yes skip,
if not then write to the file and also write to corresponding results file.*)

If[MemberQ[ExternalMemory,ObtainedData]==False ,

WriteLine[currentdirectory<>"\\"<>"external.txt", ToString[ObtainedData]];
WriteLine[currentdirectory<>"\\"<>"results"<>ToString[\$KernelID]<>".txt", ObtainedData];
];

,{iilsts, 1, 1000}];

......


One thing I thought is to use the master (KernelID=0) or one of other Kernels to read all the results files and write to external.txt, but I'm not sure how and whether it can solve.

Is anybody familiar with such problems or similar cases? Thank you all in advance!

## closed as off-topic by rhermans, m_goldberg, MarcoB, Öskå, C. E.Aug 8 at 18:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – rhermans, m_goldberg, MarcoB, Öskå
If this question can be reworded to fit the rules in the help center, please edit the question.

• Trying to have multiple programs writing in the same file without coordination seems to be calling for problems. What about a database? If not, depending on the overheads, how frequent the queries and heavy the data you can use SetSharedVariable to keep a light index synchronized on all kernels. – rhermans Jul 20 at 10:27
• I'm not sure how frequent the queries. What do you mean use SetSharedVariable to keep a light index? do you mean on all kernels I try to fix the index? Thank you very much! – Xuemei Gu Jul 20 at 10:32
• Did you read the docs for SetSharedVariable? It "declares symbols as shared variables whose values are synchronized among all parallel kernels.". If there is a lightweight way to identify your data, you can keep an index in a shared variable or a database instead of searching and writing a file each time. And then create one data file per kernel, so there no possibility of collision. – rhermans Jul 20 at 10:37
• I just read the docs for SetShareVariable. The way I do is always to write and read the file, I don't know to the lightweight way to identify data yet. I will have a try. Thank you! – Xuemei Gu Jul 20 at 15:13
• Probably the best answer is that you just shouldn't. Think twice if you really need to do this, and then think again. If you still want it after that, create a separate function for writing to the file and use SetSharedFunction on it. There's also CriticalSection. – Szabolcs Jul 20 at 16:44