# How to read text file parallel

I want to read text file parallel but I am not sure how to do it? This is what I tried to do

str = OpenRead["test.txt"];
index = Table[pos = StreamPosition[str]; Skip[str, Record]; pos, {40000}];
Parallelize[Table[SetStreamPosition[str, index[[n]]];Print[Read[str, Record, RecordSeparators -> {"\n"}]]; , {n, 1, 40000} ]];
Close[str];


But this won't work. But if I remove Parallelize part then it work(only on master kernel). I set str and index as shared variables but still it didn't work.

Any comment appreciated.

Since my question Writing parallel program to read text file and compute matrix rank drew negative criticism from users I thought to split my question to small parts and ask separately.

-
Are you CPU bound or IO bound when reading the file without parallel? Usually read-speed is limited by the drive. And the stream position can only be in one place at a time, when you run it in parallel SetStreamPosition can be set by another kernel causing the Read to occur in the wrong place. –  ssch Oct 17 at 21:05
@ssch I am not familiar with concept "CPU bound" and "IO bound". But I think what you mean is whether my program will be faster if CPU is faster or not. Yes I think so. I have more than one kernels and right now I am using only one. What I am trying to do is read few lines from text file parallel then send it to different kernels for parallel computations(which I didn't include here). Then again read few lines parallel send it to computations, etc. So as first step I tried to figure out how to read lines parallel. This is about it. –  Vajira Oct 17 at 21:26
Reading from files is IO bound by definition and is most efficient when done sequentially in large chunks, I would expect reading in parallel (even if you get it to work) to be much slower except for very special circumstances/setups. This might not be as dramatic when using a SSD as it is with a HDD, but even then I would rather suggest to sequentially read larger chunks into memory and only parallelize the processing of that data (probably including the parsing of Records). Have you any good reasons to believe that reading on your system could be sped up by parallelization? –  Albert Retey Oct 18 at 8:12
@ Albert Retey I think you are correct. I should read first large chunk to memory. then process it then read again another large chunk and so on. –  Vajira Oct 18 at 16:11