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

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

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

本人在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调用 java webservice接口

php调用 java webservice接口

php调用Webservice基本语法如下:$url ='xxxxxxx.cn'  //链接服务器端 $client = new SoapClient($url);通过以上语法已经连接到webservice,也可将wsdl在本地使用,...

php非对称加密

php非对称加密

先在centos安装openssl,然后开始://生成私钥openssl genrsa -out rsa_private_key.pem 1024//生成公钥openssl rsa -in rsa_private_key.pem&...

php mcrypt扩展被废弃的解决方案

php mcrypt扩展被废弃的解决方案

使用openssl扩展对应替换mcrypt的函数,(比较麻烦,但是openssl是未来趋势)在新版php中编译mcrypt扩展使用一个纯php代码实现的mcrypt扩展库,git地址为https://github.com/phpseclib/mcrypt_compat,每个mcrypt的方法都已经实...

php curl Received HTTP code 403 from proxy after CONNECT

php curl Received HTTP code 403 from proxy after CONNECT

在调用微信code换openid的接口curl报错curl Received HTTP code 403 from proxy after CONNECT,错误码56。可以看到是curl的代理有问题。然后我自己电脑设置代理去访问curl请求的地址,的确也返回了403,说明代理不允许访问这个地址,联系...

php redis队列

php redis队列

本篇文章是给新手学习php redis队列怎么玩的。我们模拟验证码发送,通过队列完成。(1).创建一个验证码发送接口/**  * 发送验证码  */ public function sendCaptcha() {    ...

stream_socket_accept设置非阻塞,socket_accept设置非阻塞

stream_socket_accept设置非阻塞,socket_accept设置非阻塞

编写socket服务检测是否有新连接加入都要使用stream_socket_accept和socket_accept来获取,但是默认是阻塞的,想要更换为非阻塞并不能用stream_set_blocking($socket, false),因为stream_set_blocking只是设置socket...