PHP multi-Process: Use PHP Process Control Functions (PCNTL/thread Control function)
Function reference visible: http://www.php.net/manual/zh/ref.pcntl.php
It can only be used in Unix Like OS and Windows.
-Enable-pcntl must be added during php compilation. It is recommended that you only run in CLI mode instead of running in the WEB server environment.
The following is a simple PHP multi-process test code:Copy codeThe Code is as follows: <? Php
Declare (ticks = 1 );
$ BWaitFlag = FALSE; // whether to wait for the process to end
$ IntNum = 10; // process count
$ Pids = array (); // process PID array
Echo ("Start \ n ");
For ($ I = 0; $ I <$ intNum; $ I ++ ){
$ Pids [$ I] = pcntl_fork (); // generates sub-processes and starts the test run code from the current row without inheriting the data information of the parent process.
If (! $ Pids [$ I]) {
// Subprocess Process Code segment _ Start
$ Str = "";
Sleep (5 + $ I );
For ($ j = 0; $ j <$ I; $ j ++) {$ str. = "*";}
Echo "$ I->". time (). "$ str \ n ";
Exit ();
// Subprocess Process Code segment _ End
}
}
If ($ bWaitFlag)
{
For ($ I = 0; $ I <$ intNum; $ I ++ ){
Pcntl_waitpid ($ pids [$ I], $ status, WUNTRACED );
Echo "wait $ I->". time (). "\ n ";
}
}
Echo ("End \ n ");
?>