php执行外部命令的不少,例如:exec、shell_exec、system、popen等。
我特意的讲一下popen,它跟其他函数不太一样,其他函数执行一个命令后,会等待其返回后,再向下执行,而popen则不会。
所以你想用php程序并行的处理一些业务的时候,可以用popen。
我举个例子,有两个php文件,numgame.php和numgenerate.php,numgame.php循环的生产一个数传参给numgenerate.php,用popen和shell_exec做个对比(其中numgenerate.php不做更改):
umgenerate.php
$num=$argv[1]; file_put_contents('/tmp/shell.log',$num."\t".date('H:i:s')."\n",FILE_APPEND);
1、用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、如果在执行numgenerate.php文件时,加入一个&,将其放倒后台,即将
$cmd2=popen("php -f numgenerate.php $num ",'r');
改为
$cmd2=popen("php -f numgenerate.php $num &",'r');
会发生什么变化。
3、用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、同样的,跟第2个例子一样,在其后面加个&,即将
shell_exec("php -f numgenerate.php $num ",'r');
改为
shell_exec("php -f numgenerate.php $num &",'r');
通过运行以上四个例子,shell.log的前1000条记录生成所需时间(记得每次运行完后,清空一下shell.log):
1、1分6秒
2、33秒
3、5分29秒
4、1分2秒
看吧,时间差距还是蛮大的。我觉得用&将命令放倒linux后台去执行,linux系统就不会等待其php程序执行的返回,从而更节省了时间。
更多相关教程请访问 php编程从入门到精通全套视频教程

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
