Home >Backend Development >PHP Tutorial >How does popen realize the concurrent execution of multiple processes? The pclose in the loop will wait for the process to complete before proceeding to the next loop.
1. How does PHP popen realize the concurrent execution of multiple processes? The pclose in the loop will wait for the process to complete before proceeding to the next loop
2. Assume that there are 17 processes to be started. How to start 5 processes at a time, close one process every time it is completed, and start the next process at the same time, which means that only 5 processes can be executed at the same time at most
<code>//启动2个进程 for($i = 0;$i < 2;$i++){ $command = "$phpPath $destPHPFile >> $logFile$i"; echo "进程开启时间".date('Y-m-d H:i:s')."\n"; $resource = popen($command,'r'); if(is_resource($resource)){ $success++; pclose($resource);//下一次循环会等待上一个进程执行完毕,pclose才会释放资源 echo date('Y-m-d H:i:s')." 进程:".$i."启动完毕,执行完毕并关闭,开启下一个进程\n"; }else{ $failure++; } }</code>
This approach is equivalent to starting a process each time and executing it in a loop. It is equivalent to a single process processing tasks. How to achieve multi-process
1. How does PHP popen realize the concurrent execution of multiple processes? The pclose in the loop will wait for the process to complete before proceeding to the next loop
2. Assume that there are 17 processes to be started. How to start 5 processes at a time, close one process every time it is completed, and start the next process at the same time, which means that only 5 processes can be executed at the same time at most
<code>//启动2个进程 for($i = 0;$i < 2;$i++){ $command = "$phpPath $destPHPFile >> $logFile$i"; echo "进程开启时间".date('Y-m-d H:i:s')."\n"; $resource = popen($command,'r'); if(is_resource($resource)){ $success++; pclose($resource);//下一次循环会等待上一个进程执行完毕,pclose才会释放资源 echo date('Y-m-d H:i:s')." 进程:".$i."启动完毕,执行完毕并关闭,开启下一个进程\n"; }else{ $failure++; } }</code>
This approach is equivalent to starting a process each time and executing it in a loop. It is equivalent to a single process processing tasks. How to achieve multi-process