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

简介: php windows多进程,php windows创建多进程,

本人在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);

               }

           }

       }

   }

}

目录
相关文章
|
1月前
|
PHP Docker 容器
如何在宿主主机运行容器中的php守护进程
在Docker容器中同时运行多个程序(如Nginx+PHP+Ftp)时,需用`docker exec`命令启动额外服务。首先通过`php -v`查看PHP版本,再用`which php-fpm7.4`确认PHP安装路径,通常返回`/usr/sbin/php-fpm7.4`。最后直接运行该路径启动PHP-FPM服务,确保其正常工作。
49 14
|
10天前
|
云安全 安全 PHP
PHP CGI Windows平台远程代码执行漏洞(CVE-2024-4577)
PHP CGI Windows平台远程代码执行漏洞(CVE-2024-4577)
|
2月前
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
187 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
4月前
|
PHP
PHP的pcntl多进程用法实例
PHP使用PCNTL系列的函数也能做到多进程处理一个事务。
49 12
|
5月前
|
Java 关系型数据库 MySQL
java控制Windows进程,服务管理器项目
本文介绍了如何使用Java的`Runtime`和`Process`类来控制Windows进程,包括执行命令、读取进程输出和错误流以及等待进程完成,并提供了一个简单的服务管理器项目示例。
74 1
|
6月前
|
存储 算法 Linux
C语言 多进程编程(一)进程创建
本文详细介绍了Linux系统中的进程管理。首先,文章解释了进程的概念及其特点,强调了进程作为操作系统中独立可调度实体的重要性。文章还深入讲解了Linux下的进程管理,包括如何获取进程ID、进程地址空间、虚拟地址与物理地址的区别,以及进程状态管理和优先级设置等内容。此外,还介绍了常用进程管理命令如`ps`、`top`、`pstree`和`kill`的使用方法。最后,文章讨论了进程的创建、退出和等待机制,并展示了如何通过`fork()`、`exec`家族函数以及`wait()`和`waitpid()`函数来管理和控制进程。此外,还介绍了守护进程的创建方法。
C语言 多进程编程(一)进程创建
|
6月前
|
Linux C语言
C语言 多进程编程(四)定时器信号和子进程退出信号
本文详细介绍了Linux系统中的定时器信号及其相关函数。首先,文章解释了`SIGALRM`信号的作用及应用场景,包括计时器、超时重试和定时任务等。接着介绍了`alarm()`函数,展示了如何设置定时器以及其局限性。随后探讨了`setitimer()`函数,比较了它与`alarm()`的不同之处,包括定时器类型、精度和支持的定时器数量等方面。最后,文章讲解了子进程退出时如何利用`SIGCHLD`信号,提供了示例代码展示如何处理子进程退出信号,避免僵尸进程问题。
|
7月前
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
|
7月前
|
PHP 开发工具 git
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法
|
7月前
|
Java Windows
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?