Php concurrent request count through curl

Source: Internet
Author: User
Php uses the curl concurrent request count function cumulative_number (){
$ File = "number.txt ";
$ Current = file_get_contents ($ file );
$ Current = (empty ($ current ))? 1: (int) $ current + 1;
If (! File_put_contents ($ file, $ current, LOCK_EX )){
Cumulative_number ();
}
Return $ current;
}

I want to implement the count through this function, but there is a problem. if there are multiple accesses at the same time, this count will go wrong. During simultaneous access, multiple accesses read the content in the file at the same time. although there is lock protection during writing, the same content exists in the written text, then there will be a problem with the count.

I don't know how to apply a lock-like function when reading files. Help


Reply to discussion (solution)

Http://www.phpthinking.com/archives/220

The information you provided has no effect on this issue.

Http://www.phpthinking.com/archives/220

$fn = 'count.txt';if(! file_exists($fn)) file_put_contents($fn, 1);$fp = fopen($fn, 'r+');if(flock($fp, LOCK_EX | LOCK_NB)) {  $n = fgets($fp);  rewind($fp);  fputs($fp, $n+1);}flock($fp, LOCK_UN);

Sorry, the moderator's method also seems to have problems. At the same time, there is no problem with the small number of accesses. if there are too many accesses, there will be problems.

$fn = 'count.txt';if(! file_exists($fn)) file_put_contents($fn, 1);$fp = fopen($fn, 'r+');if(flock($fp, LOCK_EX | LOCK_NB)) {  $n = fgets($fp);  rewind($fp);  fputs($fp, $n+1);}flock($fp, LOCK_UN);

After a few days of disappearance, why?
This code has the same effect as your original code. I thought there was a response no later than the next day. I didn't expect it to take so many days.
Remove LOCK_NB.

Test Code

$mh = curl_multi_init();for($i=0; $i<1000; $i++) {       $conn[$i]=curl_init("http://localhost/file_count.php?i=$i");       curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);       curl_multi_add_handle ($mh,$conn[$i]);}do { $n=curl_multi_exec($mh,$active); } while ($active);foreach ($conn as $ch) {       $res[] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) . ' ' .curl_multi_getcontent($ch);       curl_close($ch);}print_r($res);

File_count.php

  

function chatbackcurl(){    $url = "http://www.XXX.com/peter_linux.php";    for($i=0;$i<30;$i++) {        $oplink[$i]['link'] = $url;    }    $mh = curl_multi_init();    foreach($oplink as $key=>$info) {        $conn[$key]=curl_init($info['link']);        curl_setopt($conn[$key],CURLOPT_RETURNTRANSFER,1);        curl_multi_add_handle ($mh,$conn[$key]);    }    $active = null;    do {        $mrc = curl_multi_exec($mh,$active);    } while ($mrc == CURLM_CALL_MULTI_PERFORM);    while ($active and $mrc == CURLM_OK) {        if (curl_multi_select($mh) != -1) {            do {                $mrc = curl_multi_exec($mh, $active);            } while ($mrc == CURLM_CALL_MULTI_PERFORM);        }    }    foreach ($oplink as $key => $info) {        $oplink[$key]['back_status']=curl_multi_getcontent($conn[$key]);        curl_close($conn[$key]);    }    return $oplink ;}


The simulated concurrent access code is as follows:

Thanks to the moderator for helping solve the problem, but I don't know if the problem will happen when the traffic volume is large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.