当前位置:首页 > PHP > 正文内容

php windows多进程,php windows创建多进程,

高老师5年前 (2020-08-06)PHP1374

本人在windows下创建多进程的研究,唯一缺点,主进程所在终端关闭则所有子进程全部关闭。原理是通过proc_open创建多进程,通过环境变量识别父子进程,还能通过proc_open进行父子进程通信

<?php
namespace EasyTask;

use \Exception as Exception;

/**
 * Class Wpc
 * @package EasyTask
 */
class Wpc
{
    /**
     * 进程实例
     * @var resource
     */
    private $instance = null;

    /**
     * 环境变量
     * @var null
     */
    private $env = null;

    /**
     * 进程管道
     * @var null
     */
    private $pipes = [];

    /**
     * 进程工作目录
     * @var null
     */
    private $workDir = null;

    /**
     * 进程文件
     * @var null
     */
    private $filename = '';

    /**
     * 进程执行参数
     * @var null
     */
    private $argument = '';

    /**
     * 标准输入输出描述符
     * @var array
     */
    private $stdStruct = [];

    /**
     * 设置环境变量
     * @param array $env
     * @return $this
     */
    public function setEnv($env)
    {
        $this->env = $env;
        return $this;
    }

    /**
     * 设置进程文件
     * @param string $filename
     * @return $this
     * @throws Exception
     */
    public function setFile($filename)
    {
        $filename = realpath($filename);
        if (!file_exists($filename))
        {
            throw new Exception("the file:{$filename} is not exist");
        }
        $this->filename = $filename;
        return $this;
    }

    /**
     * 设置进程参数
     * @param string $argument
     * @return $this
     */
    public function setArgument($argument)
    {
        $argument = (string)$argument;
        $this->argument = $argument;
        return $this;
    }

    /**
     * 设置进程工作目录
     * @param string $path
     * @return $this
     * @throws Exception
     */
    public function setWorkDir($path)
    {
        $path = realpath($path);
        if (!is_dir($path))
        {
            throw new Exception("the path:{$path} is not exist");
        }
        $this->workDir = $path;
        return $this;
    }

    /**
     * 设置标准输入输出文件描述符
     * @param array $struct
     * @return $this
     */
    public function setStdStruct($struct)
    {
        $this->stdStruct = $struct;
        return $this;
    }

    /**
     * 获取进程ID
     * @return int|false
     */
    public function getPid()
    {
        if (is_resource($this->instance))
        {
            $status = proc_get_status($this->instance);

            return !empty($status['pid']) ? $status['pid'] : false;
        }

        return false;
    }

    /**
     * 获取程是否正在运行
     * @return bool
     */
    public function getIsRunning()
    {
        if (is_resource($this->instance))
        {
            $status = proc_get_status($this->instance);

            return !empty($status['running']) ? $status['running'] : false;
        }

        return false;
    }

    /**
     * 启动进程
     * @return int 进程id
     * @throws
     */
    public function start()
    {
        if (!$this->stdStruct)
        {
            throw new Exception('Please set the file descriptor');
        }
        $this->instance = proc_open("$this->filename $this->argument", $this->stdStruct, $this->pipes, $this->workDir, $this->env, ['bypass_shell' => true]);
        if (!$this->instance)
        {
            throw new Exception('failed to create process through proc_open');
        }

        return $this->getPid();
    }

    /**
     * 停止进程
     * @param bool $force 是否强制退出
     */
    public function stop($force = false)
    {
        if (is_resource($this->instance))
        {
            $this->closePipes();
            if (!$force)
            {
                proc_close($this->instance);
            }
            else
            {
                proc_terminate($this->instance);
            }
        }
    }

    /**
     * 关闭管道
     */
    private function closePipes()
    {
        if (is_resource($this->instance))
        {
            foreach ($this->pipes as $pipe)
            {
                if (is_resource($pipe))
                {
                    fclose($pipe);
                }
            }
        }
    }
}

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/147.html

分享给朋友:

“php windows多进程,php windows创建多进程,” 的相关文章

 php 判断是否post,php判断是否post提交,php 判断是否为post,php 判断get 还是post

php 判断是否post,php判断是否post提交,php 判断是否为post,php 判断get 还是post

if($_SERVER['REQUEST_METHOD'] == 'POST') { echo('This is  post '); } elseif ($_SERVER['...

PHP二维数组排序,PHP多维数组排序, array_multisort()

PHP二维数组排序,PHP多维数组排序, array_multisort()

使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...

PHP getenv函数和putenv函数的学习

PHP getenv函数和putenv函数的学习

(1).在PHP中可以查看的环境变量包括: (1.1).电脑环境变量 (2.1).服务器环境变量(2).getenv()函数获取一个环境变量的值.参数1是环境变量的key,参数2值为true的时候仅从你的电脑环境变量中查找,参数2值为false会从两种变量中全部查询//获取我电脑登录的用户名,输出A...

php限制方法返回值类型

php限制方法返回值类型

php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int {    &nb...

posix_ttyname函数详解

posix_ttyname函数详解

posix_ttyname - 获取当前终端设备名称。<?php     var_dump( posix_ttyname(STDOUT) );我们启动一个终端,执行上面的代码输出:/dev/tty1我们再启动一个终端,执行上面的代码输...

packagist包发布稳定版

packagist包发布稳定版

自己的composer已经发布到packagist,但是无法使用composer require easy-task/easy-task来安装,只能在配置文件使用如下方式安装:"require": {     "easy...