PHP multi-Process: use ProcessControlFunctions (PCNTL thread Control function) of PHP. if you need it, refer to PHP multi-Process: use Process Control Functions (PCNTL/thread Control function) of PHP)
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:
The code is as follows:
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 ");
?>