PHPackages                             pagon/childprocess - 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. pagon/childprocess

ActiveLibrary

pagon/childprocess
==================

PHP child process manager

0.0.5(12y ago)336234[1 issues](https://github.com/hfcorriez/php-childprocess/issues)2MITPHPPHP &gt;=5.3.0

Since Apr 6Pushed 11y ago4 watchersCompare

[ Source](https://github.com/hfcorriez/php-childprocess)[ Packagist](https://packagist.org/packages/pagon/childprocess)[ RSS](/packages/pagon-childprocess/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (6)Used By (2)

其它语言：[English](README_en.md)

PHP-ChildProcess
================

[](#php-childprocess)

一个CLI下易用简单的管理进程的库

安装
==

[](#安装)

添加 `"pagon/childprocess": "*"` 到 [`composer.json`](http://getcomposer.org):

```
composer.phar install

```

目录
==

[](#目录)

- [使用管理器](#%E4%BD%BF%E7%94%A8%E7%AE%A1%E7%90%86%E5%99%A8)
- [创建子进程](#%E5%88%9B%E5%BB%BA%E5%AD%90%E8%BF%9B%E7%A8%8B)
    - [自动运行](#%E8%87%AA%E5%8A%A8%E8%BF%90%E8%A1%8C)
    - [手动运行](#%E6%89%8B%E5%8A%A8%E8%BF%90%E8%A1%8C)
    - [手动运行等待](#%E6%89%8B%E5%8A%A8%E8%BF%90%E8%A1%8C%E7%AD%89%E5%BE%85)
    - [运行PHP文件](#%E8%BF%90%E8%A1%8CPHP%E6%96%87%E4%BB%B6)
    - [运行命令](#%E8%BF%90%E8%A1%8C%E5%91%BD%E4%BB%A4)
- [其它](#%E5%85%B6%E5%AE%83)
    - [发送消息](#%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF)
    - [高级用法](#%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95)
- [事件](#%E4%BA%8B%E4%BB%B6)
    - [ChildProcess](#childprocess%E4%BA%8B%E4%BB%B6)
    - [Process](#process%E4%BA%8B%E4%BB%B6)

使用
==

[](#使用)

使用管理器
-----

[](#使用管理器)

控制当前进程

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$manager->on('exit', function () use ($process) {
    error_log('exit');
    exit;
});

// 做其他事情或等待
```

创建子进程
-----

[](#创建子进程)

### 简单运行

[](#简单运行)

在子平行空间运行闭包函数

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$child = $manager->parallel(function () {
    sleep(10);
    // 做其他事情
});
```

> 为了保证主进程不退出来handle事件，可以使用`join`或者`while(1)`来保证运行

### 手动运行

[](#手动运行)

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$child = $manager->parallel(function () {
    // to do something
    sleep(10);
    error_log('child execute');
}, false);

$child->on('exit', function ($status) {
    error_log('child exit ' . $status);
});

// 只运行子进程不等待退出
$child->run()

while(1) { /*to do something */}
```

### 手动运行等待

[](#手动运行等待)

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$child = $manager->parallel(function () {
    // to do something
    sleep(10);
    error_log('child execute');
}, false);

$child->on('exit', function ($status) {
    error_log('child exit ' . $status);
});

// 等待子进程退出
$child->join();
```

### 运行PHP文件

[](#运行php文件)

在子平行空间运行PHP文件

主进程：

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$child = $manager->fork(__DIR__ . '/worker.php', false);

$child->on('exit', function ($status) {
    error_log('child exit ' . $status);
});

$child->join();
```

PHP文件：

```
$process // The parent process
// 需要做的工作
```

### 运行命令

[](#运行命令)

在子进程运行命令，并且捕获输出

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$child = $manager->spawn('/usr/sbin/netstat', false);

$child->on('stdout', function ($data) {
    error_log('receive stdout data: '  . $data);
    // to save data or process it
});

$child->on('stderr', function ($data) {
    error_log('receive stderr data: '  . $data);
    // to save data or process something
});

$child->join();
```

其它
--

[](#其它)

### 发送消息

[](#发送消息)

父子进程可以使用消息来通信

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$manager->listen();

$child = $manager->parallel(function (Process $process) {
    $process->on('message', function ($msg) {
        error_log('child revive message: ' . $msg);
    });

    $process->listen();

    $process->send('hello master');

    error_log('child execute');
}, false);

$child->on('message', function ($msg) {
    error_log('parent receive message: ' . $msg);
});

$child->send('hi child');

$child->join();
```

### 高级用法

[](#高级用法)

#### 设置选项

[](#设置选项)

当前支持的选项列表：

```
array(
    'cwd'      => false,        // Current working deirectory
    'user'     => false,        // Startup user
    'env'      => array(),      // Enviroments
    'timeout'  => 0,            // Timeout
    'init'     => false,        // Init callback
    'callback' => false         // Child startup callback
)
```

一些用法：

```
declare(ticks = 1) ;

$manager = new ChildProcess();

$child = $manager->spawn('/usr/sbin/netstat', array(
    'timeout' => 60 // Will wait 60 seconds
    'callback' => function(){ error_log('netstat start'); }
), false);

$child->on('stdout', function ($data) {
    echo $data;
    // to save data or process it
});

$child->join();
```

事件
--

[](#事件)

### ChildProcess事件

[](#childprocess事件)

- `tick` 每个tick都会触发，主要用于监控一些行为来及时反馈到管理器
- `listen` 监听消息队列
- `signal` 收到任何的信号
- `abort` 中断时：SIGINT,SIGTERM
- `finish` 运行结束时（正常退出）
- `exit` 管理器进程退出，包含`abort`和`finish`

### Process事件

[](#process事件)

- `listen` 当前进程实例开始监听队列时触发
- `run` 当前进程实例手动运行时
- `init` 当前进程实例子进程创建完成时
- `fork` 当前进程实例fork时
- `abort` 当前进程实例中断时：SIGINT,SIGTERM
- `finish` 当前进程实例结束时（正常退出）
- `exit` 当前进程实例退出时，包含`abort`和`finish`

授权
==

[](#授权)

[MIT](./LICENSE)

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity52

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 ~38 days

Total

5

Last Release

4632d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/052da9fcd2d35025de486c255a8b4d38a67b2626b6ca626f6681bbc00187e62b?d=identicon)[hfcorriez](/maintainers/hfcorriez)

---

Top Contributors

[![hfcorriez](https://avatars.githubusercontent.com/u/119550?v=4)](https://github.com/hfcorriez "hfcorriez (101 commits)")

---

Tags

processpcntlchildprocess

### Embed Badge

![Health badge](/badges/pagon-childprocess/health.svg)

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

###  Alternatives

[phpunit/php-invoker

Invoke callables with a timeout

1.3k540.0M285](/packages/phpunit-php-invoker)[hhxsv5/laravel-s

🚀 LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.

3.9k676.0k10](/packages/hhxsv5-laravel-s)[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)[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)
