PHP multi-process Write file

Source: Internet
Author: User
Tags php write

Test A
$begin= Time(); for($i= 0;$i<10000;$i++) {        $fp=fopen("tmp", ' r+ '); fseek($fp, 0,seek_end); fwrite($fp,str_repeat($argv[1],1024*32].Php_eol); fclose($fp);}$end= Time();Echo"Time use:". ($end-$begin).Php_eol;

PHP write.php b

PHP write.php A

WC-L tmp

10450 tmp

Test results:

Does not match the expected 2W line

Error Analysis:

Seek navigates to the end of the file, possibly because the other process has already written to the file, overwriting the current process

Test Two
$begin= Time(); for($i= 0;$i<10000;$i++) {        $fp=fopen("tmp", ' A + ');        fwrite($fp,str_repeat($argv[1],1024*32].Php_eol); fclose($fp);}$end= Time();Echo"Time use:". ($end-$begin).Php_eol;

PHP write.php b

PHP write.php A

WC-L tmp

20000 tmp

Test results:

Matches the expected 2W line, but checks the file contents

Check the script to check whether a row contains both A and b

<?PHP$fp=fopen("tmp", ' r+ '); while(!feof($fp)) {        $line=fgets($fp, 1024*1024); if(strstr($line, ' a ') &&strstr($line, ' B ')) {                Echo' Not pass '.Php_eol;  for($i= 0;$i<strlen($line);$i++){                        Echo Ord($line[$i]).Php_eol; }                 die; }}Echo' Pass '.Php_eol;

PHP check.php >ts

TS Content

Not pass9797...989810
Error Analysis:

PHP's fwrite is buffer, the content of writing a row is larger than the length of buffer, process A and process B are calling write to the same line in turn, resulting in this result

Test Three

Sequential Write

$begin= Time();$fp=fopen("tmp", ' w+ '); for($i= 0;$i<200000;$i++) {        fwrite($fp,str_repeat($argv[1],1024*32].Php_eol);}fclose($fp);$end= Time();Echo"Time use:". ($end-$begin).Php_eol;

PHP write.php A

Time Use:13

$begin= Time(); for($i= 0;$i<200000;$i++) {        $fp=fopen("tmp", ' A + '); fwrite($fp,str_repeat($argv[1],1024*32].Php_eol); fclose($fp);}$end= Time();Echo"Time use:". ($end-$begin).Php_eol;

PHP write.php A

Time Use:16

PHP multi-process Write file

Related Article

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.