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

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

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

本人在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判断微信浏览器, PHP判断是否微信浏览器

PHP判断微信浏览器, PHP判断是否微信浏览器

获取浏览器信息原理是基于浏览器的UA信息,早在AsP时代就这样判断,但是腾讯总是喜欢自己折腾,不知道以后会修改成什么! 提取自人人商城官方自带的判断方法:function is_weiXin()  {    if (empty($_SERV...

php scoket,php webscoket,php webscoket 服务器

php scoket,php webscoket,php webscoket 服务器

项目需要使用websocket推送最新订单,客户服务器非linux不支持swoole,因此使用原生,直接上代码(1).PHP服务端<?php ini_set('error_reporting', E_ALL ^ E_NOTICE); ini_set...

php引用变量的完全理解

php引用变量的完全理解

在PHP中,大部分变量类型,如字符串,整型,浮点,数组等都是值类型的,而类和对象是引用类型.和其他语言有点差距.(1).在值类型中我们直接使用&符号表示指向对应变量的内存地址,当前变量和被指向的变量只要有1个的值被修改都会直接影响另外一个变量的值发生变化。(ps:还是非常节省内存的,可以使用...

php异步执行,php后台运行,如何在windows下让php后台运行

php异步执行,php后台运行,如何在windows下让php后台运行

如果想在windows中执行php,并且让php脚本在后台运行,可以用下面的cmd命令start /b php  D:\wwwroot\default\demo1\run.php例如上面的命令意思后台运行run.php,如果想用php编写异步代码: ...

php迭代器学习

php迭代器学习

php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.使用迭代器和普通循环实现range()函数。(1).普通循环实现range()函数。function newrange($low, $h...

php数组合并 array_merge和+号的区别

php数组合并 array_merge和+号的区别

array_merge是最常用的数组合并方法,+号同样也可以,但是却有很大不同。array_merge遇到相同字符串key,后面数组的key会覆盖前面数组的key,+号正好相反。$a = [ 'one' => 'A on...