本人在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); } } } } }
php arrayaccess 官方的说法是让你能以数组的形式访问对象,对于这种php内置接口一直不太明白有什么用,坚持多看文章,终于理解,特来分享,思路不同,更易于理解。(1).创建一个学生类,并且实现arrayaccess 接口。<?php class studen...
断点下载的原理:http请求头添加Range参数告诉文件服务器端需要的字节范围例如1个文本文件的字节为1000,第一次请求Range: bytes=0-500第二次请求Range: bytes=501-1000通过每次的请求将返回的流追加写入到文件。注意的项目:断点下载服务器端的每次只返回字节传输的...
众所周知MyISAM引擎不支持事务,但是我只是知道不支持事务,并未测试具体的表现是什么,测试代码如下:try { //开启事务 Db::startTrans(); &...
PHP简单定时器可以通过pcntl_signal创建闹钟信号来实现。但是缺点很明显,性能一般,要自己实现守护进程,不支持毫秒级定时器,单进程不支持多个闹钟信号,不能跨平台运行event扩展支持的事件多,性能高。<?php //创建event配置.[空配置] $eventConfig ...
php生成器的方法getReturn获取生成器迭代完成后的返回值,当生成器迭代完成会将生成器的返回值返回,因此如果迭代器未进行迭代是获取不到值的,如果你没有return值则返回null,参考代码:<?php function G1() { &nbs...
由于workerman底层直接读取$_SERVER['argv']的命令行参数,没有提供独立的方法start/stop,而tp的命令行参数无法适配workerman,虽然thinkphp官方专门做了一个适配的版本,但是看了下评论问题挺多的。于是自己来搞一个.(1).在applicat...