简体   繁体   中英

How can i run php script concurrently?

I want to run this piece of code concurrently. There is a txt file in the directory named as id.txt where all parameters are replace in the Cookie in headers below. If I'm running this by opening 10 cmd tabs then the process gets slow. So please help me how can I run this concurrently or any suggestions by you

$i=trim(file("index.txt")[0])+0;
$id=file("id.txt")[$i];
$idd=preg_replace('/\s+/', '', $id);
file_put_contents("index.txt",$i+1);
echo $idd;  
while(true){

$url8 = "https://www.example.com/";
$header= array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                 "Accept-Language: en-US,en;q=0.5",
                 "Content-Type: application/x-www-form-urlencoded",
                 "Content-Length: 0",
                 "Cookie: $idd");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url8);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0");
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$out = curl_exec($ch);
echo "\n";
echo $currentTime = date( 'h i s', time () );
if(preg_match('/done/',$out)){
    die(' done');   
}
}

As I see, it gets slow because all the instances of the script you run are accessing the same file simultaneously, so there is a delay when obtaining the handle needed for accessing the file. You should use different filenames (ex. index1.txt, index1337.txt) and avoid writing to the same file with multiple instances.

I hope my answer helped :)

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