A lightweight mulit-process helper base on PHP.

Overview

workbunny

workbunny/process

🐇 A lightweight multi-process helper base on PHP. 🐇

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

简介

这是一个基于ext-pcntl和ext-posix拓展的PHP多进程助手,用于更方便的调用使用。

快速开始

composer require workbunny/process
  • 创建一个子Runtime
// 使用对象方式
$p = new \WorkBunny\Process\Runtime();
$p->fork(function(){
    var_dump('child');
});
  • 父Runtime执行
$p = new \WorkBunny\Process\Runtime();

$p->parent(function(){
    var_dump('parent'); # 仅输出一次
});
  • 快速创建运行多个子Runtime
$p = new \WorkBunny\Process\Runtime();

$p->run(function(){
    var_dump('child');
},function(){
    var_dump('parent');
}, 4); # 1 + 4 进程
  • 监听子Runtime
$p = new \WorkBunny\Process\Runtime();

$p->wait(function(\WorkBunny\Process\Runtime $parent, int $status){
    # 子进程正常退出则会调用该方法,被调用次数是正常退出的子进程数量
},function(\WorkBunny\Process\Runtime $parent, $status){
    # 子进程异常退出则会调用该方法,被调用次数是异常的子进程数量
});

方法

注:作用范围为父Runtime的方法仅在父Runtime内有有效响应

方法名 作用范围 是否产生分叉 描述
fork() 父Runtime 分叉一个子Runtime
run() 父Runtime 快速分叉N个子Runtime
wait() 父Runtime × 监听所有子Runtime状态
parent() 父Runtime × 为父Runtime增加回调响应
isChild() 所有 × 判断是否是子Runtime
getId() 所有 × 获取当前Runtime序号
getPid() 所有 × 获取当前RuntimePID
getPidMap() 父Runtime × 获取所有子RuntimePID
number() 父Runtime × 获取Runtime数量 or 产生子Runtime自增序号
setConfig() 所有 且 分叉发生前 × 设置config
getConfig() 所有 × 获取config
getPidMap() 父Runtime × 获取所有子RuntimePID
setPriority() 所有 × 为当前Runtime设置优先级 需要当前执行用户为super user
getPriority() 所有 × 获取当前Runtime优先级
exit() 所有 × 进程退出

说明

1. 初始化

  • Runtime对象初始化支持配置
    • pre_gc :接受bool值,控制Runtime在fork行为发生前是否执行PHP GC;注:Runtime默认不进行gc
    • priority:接受索引数组,为所有Runtime设置优先级,索引下标对应Runtime序号; 如实际产生的Runtime数量大于该索引数组数量,则默认为0;

注:fork()的priority参数会改变该默认值

注:priority需要当前用户为super user

$p = new \WorkBunny\Process\Runtime([
    'pre_gc' => true,
    'priority' => [
        0,  // 主Runtime优先级为0
        -1, // id=1的子Runtime优先级为-1
        -2, // id=2的子Runtime优先级为-2
        -3  // id=3的子Runtime优先级为-3
    ]
]);

2. fork行为

  • fork 行为发生后,Runtime对象会产生两个分支

    • id=0 的父Runtime
    • id=N 的子Runtime
  • fork()run() 之后的代码域会被父子进程同时执行,但相互隔离:

$p = new \WorkBunny\Process\Runtime();
$p->fork(function(\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id !== 0
});

var_dump('parent'); # 打印两次
$p = new \WorkBunny\Process\Runtime();
$p->run(function (\WorkBunny\Process\Runtime $runtime){
    
},function(\WorkBunny\Process\Runtime $runtime){

}, 4);

var_dump('parent'); # 打印5次
  • 如需在子Runtime中进行 fork 操作,请创建新的Runtime;不建议过多调用,因为进程的开销远比线程大
$p = new \WorkBunny\Process\Runtime();
$p->fork(function(\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id !== 0
    var_dump('old-child');
    
    $newP = new \WorkBunny\Process\Runtime();
    $newP->fork(function(\WorkBunny\Process\Runtime $newP){
        var_dump($newP->getId()); # id === 0
        var_dump('new-parent');
    });
});
# run 方法同理

3. 指定执行

  • 指定某个id的Runtime执行
$p = new \WorkBunny\Process\Runtime();
$p->run(function (){},function(){}, 4);

if($p->getId() === 3){
    var_dump('im No. 3'); # 仅id为3的Runtime会生效
}

# fork同理
  • 指定所有子Runtime执行
$p = new \WorkBunny\Process\Runtime();
$p->run(function (){},function(){}, 4);

if($p->isChild()){
    var_dump('im child'); # 所有子Runtime都生效
}

# fork同理
  • 指定父Runtime执行
$p = new \WorkBunny\Process\Runtime();
$p->run(function (){},function(){}, 4);

if(!$p->isChild()){
    var_dump('im parent'); # 父Runtime都生效
}

# 或以注册回调函数来执行
$p->parent(function(\WorkBunny\Process\Runtime $parent){
    var_dump('im parent');
});

# fork同理

4. 回调函数相关

  • 所有注册的回调函数都可以接收当前的Runtime分支对象:
$p = new \WorkBunny\Process\Runtime();
$p->fork(function(\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id !== 0
});
$p->parent(function (\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id === 0
});

$p->run(function (\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id !== 0
},function(\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id === 0
}, 4);
  • 注:注册的父Runtime回调函数内传入的是父Runtime对象,注册的子Runtime回调函数内传入的参数是子Runtime对象
$p = new \WorkBunny\Process\Runtime();
$p->fork(function(\WorkBunny\Process\Runtime $runtime){
    var_dump('child'); # 生效
    
    $runtime->fork(function(){
        var_dump('child-child'); # 由于fork作用范围为父Runtime,所以不生效
    });
});

$p->parent(function (\WorkBunny\Process\Runtime $runtime){
    var_dump('parent'); # 生效

    $runtime->fork(function(){
        var_dump('parent-child'); # 生效
    });
});

# run 方法同理

5. 其他

  • 获取当前Runtime数量

注:该方法仅父Runtime生效

$p = new \WorkBunny\Process\Runtime();
var_dump($p->number(false)); # 仅父Runtime会输出
  • 获取当前RuntimePID

注:该方法可结合指定执行区别获取

$p = new \WorkBunny\Process\Runtime();
var_dump($p->getPid()); # 所有Runtime会输出
  • 阻塞监听

注:该方法仅父Runtime生效

注:该方法在会阻塞至所有子Runtime退出

$p = new \WorkBunny\Process\Runtime();
$p->wait(function(\WorkBunny\Process\Runtime $runtime, $status){
    # 子Runtime正常退出时
}, function(\WorkBunny\Process\Runtime $runtime, $status){
    # 子Runtime异常退出时
});
  • 进程退出

注:该方法可结合指定执行区别获取

$p = new \WorkBunny\Process\Runtime();

$p->exit(0, 'success');
You might also like...
Modello base con tutto il docker configurato per php7.4, mariadb, vue3, apache...con esempi di component e chiamate rest interne

Applicazione base per utilizzare laravel con docker, php7.4, apache, mariadb10, vue3 Semplice installazione corredate di rotte web e api di base, 3 co

PPM is a process manager, supercharger and load balancer for modern PHP applications.
PPM is a process manager, supercharger and load balancer for modern PHP applications.

PPM - PHP Process Manager PHP-PM is a process manager, supercharger and load balancer for PHP applications. It's based on ReactPHP and works best with

A visual process builder
A visual process builder

DataStory ⚡ visual programming DataStory provides a workbench for designing data flow diagrams. ⚠️ We have moved to an organisation 👇 Live Demo data-

This extension facilitates the cms editing process in your store.
This extension facilitates the cms editing process in your store.

Magenerds_PageDesigner This extension facilitates the cms editing process in your store. Instead of just a wysiwyg editor you now have a drag and drop

This composer plugin is a temporary implementation of using symbolic links to local packages as dependencies to allow a parallel work process

Composer symlinker A Composer plugin to install packages as local symbolic links. This plugin is a temporary implementation of using symbolic links to

A library for simplifying the PHAR build process.

Box is a library built on the Phar class. It is designed to make it easier to create new phars and modifying existing ones. Features include compacting source files, better custom stub generation, and better OpenSSL signing handling.

An async process dispatcher for Amp.

process This package provides an asynchronous process dispatcher that works on all major platforms (including Windows). As Windows pipes are file hand

Plivo PHP Helper Library

plivo-php The Plivo PHP SDK makes it simpler to integrate communications into your PHP applications using the Plivo REST API. Using the SDK, you will

A PHP 7 value objects helper library.

valueobjects Requirements Requires PHP = 7.1 Installation Through Composer, obviously: composer require funeralzone/valueobjects Extensions This lib

Releases(1.1.0)
Owner
workbunny
An open source group that has nothing to do.
workbunny
Pour m'entraîner et apprendre php et mysql j'ai fait un site pour la base de données sakila

Site pour la base de données de Sakila But de l'exercice S'améliorer en php et mysql. Pourquoi Dans le cadre d'un exercice en PHP dans mon école, nous

null 0 Sep 20, 2022
QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific.

QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific. It is part of the QuidPHP package and can also be used standalone.

QuidPHP 4 Jul 2, 2022
Trabajo final de la materia Bases de Datos 1. Creación de una base de datos con MySQL y desarrollo de una página web con PHP para manipularla. UNAL sede Medellín, semestre 2022-1.

Trabajo final BD: i-Lunch Materia: Bases de Datos I Profesor: Francisco Javier Moreno Arboleda Institución: Universidad Nacional de Colombia sede Mede

Emmanuel López Rodríguez 2 Jul 9, 2022
A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

null 272 Dec 22, 2022
A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

null 258 Sep 15, 2022
MageVagrant - Vagrant/Chef base box for running Magento

MageVagrant MageVagrant is a complete LAMP development environment for Magento. Specially created for the Magento Developer's Guide book. Features Aut

null 59 Sep 28, 2021
Twitter Bootstrap base theme for Magento

Magento Bootstrap This is an adaption of the Twitter Bootstrap framework for the Magento Commerce system. PLEASE NOTE THIS IS A WORK IN PROGRESS Insta

Casper Valdemar Poulsen 84 Oct 31, 2022
Magento 2 base theme for sharing features across multiple themes

MASE2 Optimus theme About and purpose Optimus is a free and home-made Magento 2 theme, developed by Studio Emma . Its purpose is providing a starting

Studio Emma 87 Oct 7, 2022
PluXml, Moteur de Blog et CMS à l'XML sans base de données

PluXml Créez un site web performant en toute simplicité et sans base de données. Télécharger PluXml 5.8.7 (zip) Version bugfix (5.8.8) en développemen

PluXml 192 Dec 14, 2022