PHP开启多进程

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

核心代码:

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";
}
?>
相关推荐
zh_xuan1 小时前
Android Looper源码阅读
android
坐吃山猪3 小时前
SpringBoot01-配置文件
java·开发语言
晚风(●•σ )3 小时前
C++语言程序设计——06 字符串
开发语言·c++
我叫汪枫3 小时前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio
Nicole-----3 小时前
Python - Union联合类型注解
开发语言·python
晚云与城3 小时前
今日分享:C++ -- list 容器
开发语言·c++
兰雪簪轩3 小时前
分布式通信平台测试报告
开发语言·网络·c++·网络协议·测试报告
FPGAI4 小时前
Qt编程之信号与槽
开发语言·qt
Swift社区5 小时前
从 JDK 1.8 切换到 JDK 21 时遇到 NoProviderFoundException 该如何解决?
java·开发语言
0wioiw05 小时前
Go基础(④指针)
开发语言·后端·golang