简体   繁体   中英

php pthreads reading a file line by line

I'm trying to read a file line by line, and run each line through my code cycle, all of this works fine already, but I'm wondering how I can multithread this properly using pthreads.... I already have pthreads installed, I just need to see an example on how to read a file line by line, and run my code cycle for each line, without running the same line twice because of multithreading....

Is this possible, or am I wasting my time? These files I'm running the php script on are big, and can be over 150,000 lines....

$file = file('list.txt');
//For each file as "kek"/
ForEach ($file as $kek){
//Split the new line.
    $splitter = explode(":", $kek);
    $var1 = $splitter[0];
    $var2 = $splitter[1];
    $var3 = $splitter[2];
    $var4 = $splitter[3];

    //execute the rest of the code that you need the separated vars for....
}

edit: So, I've done a little bit of reading, and I believe what I need is a pool.... I currently have php 5.5 installed, and pthreads v2, is a pool what I need?

You can use command split in Linux , it can be split you file txt become 2 files or more what you want.

You can try command

split -l 500 file.txt -> for 500 lines each file split split -C 10M file.txt -> for 10 MB size of each file split

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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