PHP itself does not support multithreading, multi-process support is not particularly good, online to find a multi-process code (http://jasonyu.cn/html/2010/294.html), write a test program simple test a bit, you can run, however, it is far from going online.
<? Php/*** Project: Signfork: php multi-threaded library * File: Signfork. class. php */class Signfork {/*** sets the sub-process communication file directory * @ var string */private $ tmp_path = '/tmp /'; /*** main startup method of the Signfork engine * 1. Determine the $ arg type. When the type is array, the value is passed to each sub-process. When the type is numeric, number of processes to be created. * @ param object $ obj: Execution object * @ param string | array $ arg is used for parameters executed by the _ fork method in the object. * For example, $ arg is automatically decomposed: $ obj->__ fork ($ arg [0]), $ obj->__ fork ($ arg [1])… * @ Return array returns array (sub-process sequence => sub-process execution result); */public function run ($ obj, $ arg = 1) {if (! Method_exists ($ obj, '_ fork') {exit ('method "_ fork" not found! ');} If (is_array ($ arg) {$ I = 0; foreach ($ arg as $ key => $ val) {$ spawns [$ I] = $ key; $ I ++; $ this-> spawn ($ obj, $ key, $ val );} $ spawns ['Total'] = $ I;} elseif ($ spawns = intval ($ arg) {for ($ I = 0; $ I <$ spawns; $ I ++) {$ this-> spawn ($ obj, $ I) ;}} else {exit ('bad argument! ');} If ($ I> 1000) exit ('too your spawns! '); Return $ this-> request ($ spawns);}/*** Signfork Main Process Control Method * 1. $ tmpfile checks whether the sub-process file exists, if the sub-process exists, the sub-process is executed and the content * 2 and $ data is read to collect the sub-process running results and data, it is used to finally return * 3. Delete the sub-process file * 4. Round-Robin for 0.03 seconds until all sub-processes are completed, clean sub-process resources * @ param string | array $ arg is used to correspond to the ID of each sub-process * @ return array returns array ([sub-process sequence] => [sub-process execution result]); */private function request ($ spawns) {$ data = array (); $ I = is_array ($ spawns )? $ Spawns ['Total']: $ spawns; for ($ ids = 0; $ ids <$ I; $ ids ++) {while (! ($ Cid = pcntl_waitpid (-1, $ status, WNOHANG) usleep (30000); $ tmpfile = $ this-> tmp_path. 'sfpid _'. $ cid; $ data [$ spawns ['Total']? $ Spawns [$ ids]: $ ids] = file_get_contents ($ tmpfile); unlink ($ tmpfile);} return $ data ;} /*** Signfork sub-process execution Method * 1. pcntl_fork generates sub-process * 2. file_put_contents splits '$ obj->__ fork ($ val) 'execution result is saved to the text named by the specific sequence * 3. posix_kill kills the current process * @ param object $ obj the object to be executed * @ param object $ I the sequence ID of the child process, in this way, the corresponding sub-process data * @ param object $ param is used to input the $ obj method '_ fork' execution parameter */private function spawn ($ obj, $ I, $ param = null) {if (pcntl_fork () === 0) {$ ci D = getmypid (); file_put_contents ($ this-> tmp_path. 'sfpid _'. $ cid, $ obj->__ fork ($ param); posix_kill ($ cid, SIGTERM); exit ;}}?>
The test procedure is as follows:
<?phprequire_once(dirname(__FILE__) . '/Signfork.class.php');class ExecObj{ public function __fork($param){ return getmypid(); } }echo getmypid()."\n";$signfork = new Signfork();$result = $signfork->run(new ExecObj(),5);foreach ($result as $key=>$val){ echo $key." ".$val."\n";}?>