您的位置:首页 > 文旅 > 旅游 > PHP开启多进程

PHP开启多进程

2024/10/6 16:23:48 来源:https://blog.csdn.net/nece001/article/details/141033158  浏览:    关键词:PHP开启多进程

amphp/parallel是一个可以在PHP中并行处理任务的库。你可以使用它来启动和管理子进程。

核心代码:

<?php
$phpCode = <<<'PHP'
<?php
// 子进程:初始化状态
$status = "wait";// 子进程:创建一个循环,等待指令
while (true) {// 从 stdin 读取指令$command = trim(fgets(STDIN));if ($command === "stop") {$status = "stopped";echo "Process stopped\n";break;} elseif ($command === "status") {echo "Current status: $status\n";} elseif ($command === "start") {$status = "running";echo "Process started\n";} else {echo "Unknown command: $command\n";}
}
?>
PHP;// 创建一个临时文件来存储 PHP 代码
$tempFile = tempnam(sys_get_temp_dir(), 'phpcode');
file_put_contents($tempFile, $phpCode);$descriptorspec = array(0 => array("pipe", "r"),  // stdin 是一个管道,由子进程读取1 => array("pipe", "w"),  // stdout 是一个管道,由子进程写入2 => array("pipe", "w")   // stderr 是一个管道,由子进程写入
);// 启动子进程
$process = proc_open("php $tempFile", $descriptorspec, $pipes);if (is_resource($process)) {// 设置 stdout 为非阻塞模式stream_set_blocking($pipes[1], false);// 父进程:发送指令给子进程并读取输出function sendCommand($pipes, $command) {fwrite($pipes[0], $command . "\n");fflush($pipes[0]); // 确保数据被发送$output = '';$timeout = 2; // 超时时间(秒)$start_time = time();// 使用 fgets 逐行读取子进程的输出while (true) {$line = fgets($pipes[1]);if ($line === false) {// 检查是否超时if (time() - $start_time > $timeout) {break;}usleep(100000); // 等待子进程处理} else {$output .= $line;if (strpos($line, "\n") !== false) {break;}}}return $output;}echo sendCommand($pipes, "status");echo sendCommand($pipes, "start");echo sendCommand($pipes, "status");echo sendCommand($pipes, "stop");// 关闭管道fclose($pipes[0]);fclose($pipes[1]);fclose($pipes[2]);// 关闭子进程并获取退出码$return_value = proc_close($process);// 删除临时文件unlink($tempFile);echo "Command returned $return_value\n";
}
?>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com