PHPackages                             workbunny/process - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. workbunny/process

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

workbunny/process
=================

A lightweight multi-process helper base on PHP.

1.2.1(3y ago)19103MITPHPPHP ^7.4 | ^8.0

Since Jun 19Pushed 3y ago1 watchersCompare

[ Source](https://github.com/workbunny/process)[ Packagist](https://packagist.org/packages/workbunny/process)[ Docs](https://github.com/process)[ RSS](/packages/workbunny-process/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (8)Used By (0)

[![workbunny](https://camo.githubusercontent.com/e2b1164338390ab45783434659e3e419e0c3b50fbe140c697ba1f82f59015ad6/68747470733a2f2f6368617a366368657a2e636e2f696d616765732f776f726b62756e6e792d6c6f676f2e706e67)](https://camo.githubusercontent.com/e2b1164338390ab45783434659e3e419e0c3b50fbe140c697ba1f82f59015ad6/68747470733a2f2f6368617a366368657a2e636e2f696d616765732f776f726b62756e6e792d6c6f676f2e706e67)

**workbunny/process**

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

 [ ![Build Status](https://github.com/workbunny/process/actions/workflows/CI.yml/badge.svg) ](https://github.com/workbunny/process/actions) [ ![PHP Version Require](https://camo.githubusercontent.com/98b2e130ecfa6154853f31806a4c2dc620111aa6d1a9b3a1666e3863ddcbb210/687474703a2f2f706f7365722e707567782e6f72672f776f726b62756e6e792f70726f636573732f726571756972652f706870) ](https://github.com/workbunny/process/blob/main/composer.json) [ ![GitHub license](https://camo.githubusercontent.com/71bdeea7dd289eaf4ebe815f583dc91480a0dd96733e85a141e162b9ceb6a9a7/687474703a2f2f706f7365722e707567782e6f72672f776f726b62756e6e792f70726f636573732f6c6963656e7365) ](https://github.com/workbunny/process/blob/main/LICENSE)

简介
==

[](#简介)

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

快速开始
====

[](#快速开始)

```
composer require workbunny/process

```

- 创建一个子Runtime

```
// 使用对象方式
$p = new \WorkBunny\Process\Runtime();
$p->child(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内有有效响应**

方法名作用范围是否产生分叉描述child()parentContext√分叉一个子Runtime / 替换一个子Runtimerun()parentContext√快速分叉N个子Runtimewait()parentContext×监听所有子Runtime状态parent()parentContext×为父Runtime增加回调响应isChild()public×判断是否是子RuntimegetId()public×获取当前Runtime序号getPid()public×获取当前RuntimePIDgetPidMap()parentContext×获取所有子RuntimePIDnumber()parentContext×获取Runtime数量 or 产生子Runtime自增序号setConfig()public×设置configgetConfig()public×获取configgetPidMap()parentContext×获取所有子RuntimePIDsetPriority()public×为当前Runtime设置优先级 **需要当前执行用户为super user**getPriority()public×获取当前Runtime优先级exit()public×进程退出说明
==

[](#说明)

1. 初始化
------

[](#1-初始化)

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

**注：child()的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行为
---------

[](#2-fork行为)

- 在 **fork** 行为发生后，Runtime对象会产生两个分支

    - id=0 的父Runtime
    - id=N 的子Runtime
- **child()** 和 **run()** 之后的代码域会被父子进程同时执行，但相互隔离：

```
$p = new \WorkBunny\Process\Runtime();
$p->child(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次
```

- **child()** 函数可以进行替换子Runtime行为

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

// 创建一个子Runtime
// 假设父RuntimeID === 0，子RuntimeID === 1
// 假设父RuntimePID === 99，子RuntimePID === 100
$id = $p->child(function(\WorkBunny\Process\Runtime $runtime){
    $runtime->getId(); // 假设 id === 1
    $runtime->getPid(); // 假设 pid === 100
});

if($p->isChild()){
    $id === 0; // $id 在子Runtime的上下文中始终为0
    posix_getpid() === 100;
}else{
    $id === 1;// $id 在当前父Runtime的上下文中为1
    posix_getpid() === 99;
}

// 对id === 1的子Runtime进行替换
// 该用法会杀死原id下的子Runtime并新建Runtime替换它
// 该方法并不会改变子Runtime的id，仅改变id对应的pid
$newId = $p->child(function(\WorkBunny\Process\Runtime $runtime){
    $runtime->getId(); # id === 1
}, 0, $id);

if($p->isChild()){
    $id === $newId === 0;
    posix_getpid() !== 100; // 子Runtime PID发生变化，不再是100
    // 原PID === 100的子Runtime被kill
}else{
    $id === $newId === 1; // $id 没有发生变化
    posix_getpid() === 99;
}
```

- 如需在子Runtime中进行 **fork** 操作，请创建新的Runtime；**不建议过多调用，因为进程的开销远比线程大**

```
$p = new \WorkBunny\Process\Runtime();
$id = $p->child(function(\WorkBunny\Process\Runtime $runtime){
    var_dump($runtime->getId()); # id !== 0
    var_dump('old-child');

    $newP = new \WorkBunny\Process\Runtime();
    $newP->child(function(\WorkBunny\Process\Runtime $newP){
        var_dump($newP->getId()); # id === 0
        var_dump('new-parent');
    });
});
# run 方法同理
```

3. 指定执行
-------

[](#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. 回调函数相关
---------

[](#4-回调函数相关)

- 所有注册的回调函数都可以接收当前的Runtime分支对象：

```
$p = new \WorkBunny\Process\Runtime();
$p->child(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->child(function(\WorkBunny\Process\Runtime $runtime){
    var_dump('child'); # 生效

    $runtime->child(function(){
        var_dump('child-child'); # 由于fork作用范围为父Runtime，所以不生效
    });
});

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

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

# run 方法同理
```

5. 其他
-----

[](#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();

// $id RuntimeID
// $pid 进程PID
// $status 进程退出状态
$p->wait(function($id, $pid, $status){
    # 子Runtime正常退出时
}, function($id, $pid, $status){
    # 子Runtime异常退出时
});
```

- 非阻塞监听

**注：该方法仅父Runtime生效**

**注：该方法应配合event-loop的timer或者future进行监听**

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

// $id RuntimeID
// $pid 进程PID
// $status 进程退出状态
$p->listen(function($id, $pid, $status){
    # 子Runtime正常退出时
}, function($id, $pid, $status){
    # 子Runtime异常退出时
});
```

- 进程退出

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

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

$p->exit(0, 'success');
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~53 days

Recently: every ~67 days

Total

7

Last Release

1109d ago

Major Versions

0.0.2 → 1.0.02022-08-09

PHP version history (3 changes)0.0.1PHP &gt;=7.4

1.2.0PHP &gt;=8.1

1.2.1PHP ^7.4 | ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d1b317277226c93c11984f73dfad75533893466b02d06611126aa1687209c6ab?d=identicon)[Chaz6Chez](/maintainers/Chaz6Chez)

---

Top Contributors

[![chaz6chez](https://avatars.githubusercontent.com/u/22535862?v=4)](https://github.com/chaz6chez "chaz6chez (25 commits)")

---

Tags

multi-processprocessmulti-processpcntl\_fork

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/workbunny-process/health.svg)

```
[![Health](https://phpackages.com/badges/workbunny-process/health.svg)](https://phpackages.com/packages/workbunny-process)
```

###  Alternatives

[react/child-process

Event-driven library for executing child processes with ReactPHP.

34076.1M136](/packages/react-child-process)[cocur/background-process

Start processes in the background that continue running when the PHP process exists.

2971.9M12](/packages/cocur-background-process)[phpmentors/workflower

A BPMN 2.0 workflow engine for PHP

70652.9k4](/packages/phpmentors-workflower)[graze/supervisor

:vertical\_traffic\_light: Process supervisor for PHP.

999.3k](/packages/graze-supervisor)[duncan3dc/fork-helper

Simple class to fork processes in PHP and allow multi-threading

73548.0k4](/packages/duncan3dc-fork-helper)[arara/process

Provides a better API to work with processes on Unix-like systems

16861.7k2](/packages/arara-process)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
