PHP executes external commands

Source: Internet
Author: User

PHP executes many external commands, such as exec, shell_exec, System, Popen, and so on.
I specifically said Popen, which is not the same as the other functions, the other functions after executing a command, will wait for it to return, and then execute down, and Popen will not.
So you want to use PHP program in parallel to the processing of some business, you can use Popen.
I give an example, there are two PHP files, numgame.php and numgenerate.php,numgame.php loop production a number of parameters to numgenerate.php, with Popen and Shell_ EXEC makes a comparison (where numgenerate.php does not make changes):
numgenerate.php

$num=$argv[1];
file_put_contents(‘/tmp/shell.log‘,$num."\t".date(‘H:i:s‘)."\n",FILE_APPEND);

1, with Popen:

numgame.php:
function getnum($num){
$cmd=popen("ps -ef | grep ‘php -f numgenerate.php‘| wc -l",‘r‘);
$currentnum=fgets($cmd,512);
fclose($cmd);
$maxnum=20;
if($currentnum>$maxnum){
sleep(5);
getnum($num);
}else{
$cmd2=popen("php -f numgenerate.php $num ",‘r‘);
fclose($cmd2);
}

}

for($i=1111;$i<=99999;$i++){
if($i%1111!=0 && strpos($i,‘0‘)===false){
getnum($i);
}
}

2, if the implementation of the numgenerate.php file, add a &, and put it down backstage, will
$cmd 2=popen ("Php-f numgenerate.php $num", ' R ');
Switch
$cmd 2=popen ("Php-f numgenerate.php $num &", ' R ');
What will happen.

3, with Shell_exec:
numgame.php:
function getnum($num){
$currentnum=shell_exec("ps -ef | grep ‘php -f numgenerate.php‘| wc -l",‘r‘);

$maxnum = 20;
if ($currentnum > $maxnum) {
Sleep (5);
Getnum ($num);
}else{
Shell_exec ("Php-f numgenerate.php $num", ' R ');

}

}

for($i=1111;$i<=99999;$i++){
if($i%1111!=0 && strpos($i,‘0‘)===false){
getnum($i);
}
}

4. Similarly, as in the case of the 2nd example, add a & behind it;
Shell_exec ("Php-f numgenerate.php $num", ' R ');
Switch
Shell_exec ("Php-f numgenerate.php $num &", ' R ');

By running the above four examples, the first 1000 records of Shell.log generate the required time (remember to clear out the shell.log after each run):

1, 1 minutes, 6 seconds
2, 33 seconds
3, 5 minutes, 29 seconds
4, 1 minutes, 2 seconds

Look, the time gap is still pretty big. I think the Linux system will not wait for the return of its PHP program to execute with the & command down the Linux backend, thus saving time.

Reference article:
Test cases for PHP multi-process concurrency control
Execute commands and pipelines in PHP and interprocess communication (honestly, the part about the principle of implementation is not very much understood)

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.