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.

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.

WBOY
Release: 2016-10-19 10:40:50
Original
1749 people have browsed it

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>
Copy after login
Copy after login

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

Reply content:

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template