jenner/simple_fork
最新稳定版本:1.2.2
Composer 安装命令:
composer require jenner/simple_fork
包简介
simple multi process manager based on pcntl
README 文档
README
中文README.MD
Simple Fork Framework is based on PCNTL extension, the interfaces are like Thread and Runnable in Java.
Why SimpleFork
Writing Multi-Processes programs are hard for freshman. You must consider that how to recover zombie processes, interprocess communication, especially handle the process signal. SimpleFork framework provide several interfaces which like Java Thread and solutions in process pool, sync and IPC. You do not need to care about how to control multi-processes.
Require
composer require jenner/simple_fork
Or
require '/path/to/simple-fork-php/autoload.php'
Dependencies
must
- php > 5.3.0
- ext-pcntl process control
optional
- ext-sysvmsg message queue
- ext-sysvsem semaphore
- ext-sysvshm shared memory
- ext-redis redis cache and redis message queue
Property
- Process Pool and Fixed Pool
- Recover zombie process automatically
- shared memory, system v message queue, semaphore lock, file lock, redis cache, redis queue
- Three ways to make Process: extends Process, implements Runnable or create a process object with a callback function
- You can get the status of sub process
- You can stop any processes if you want, or just shutdown all processes
- You can reload the processes by reload() method, then the processes will exit and start new processes instead.
Process Pool
There are two pool you can use when you have more than one process or task to manage:Pool and FixedPool.
- Pool: you can execute different processes in one Pool object. and call the
waitmethod to wait for all the sub processes exiting (or just do something else, but do not forget to call thewaitmethod) - ParallelPool: it will keep the sub processes count, you should not init any socket connection before the FixedPool start(share socket connection is dangerous in multi processes).This class has a method
reloadwhich can reload all the sub processes. When you callreloadmethod, the master will start new N processes and shutdown the old ones. - SinglePool: no matter how many processes you execute, it will always keep one process starting and start another after it stopped.
- FixedPool: no matter how many processes you execute, it will always keep N processes starting and start another after it stopped. the active processes' count is less then N+1 forever.
Notice
- Remember that you should call the
Process::dispatchSignalmethod to call call signal handlers for pending signals. - It is not recommend that adding
declare(ticks=n);at the start of program to handle the pending signals. - A better way to handle the single is that calling
pcntl_signal_dispatchinstead ofdeclarewhich is more is a waste of CPU resources - If the sub processes exit continually and quickly, you should set
nto a small integer, else set a big one to save the CPU time. - If you want to register signal handler in the master process, the child will inherit the handler.
- If you want to register signal handler in the child process before it start, you can call the
Process::registerSignalHandlermethod.startmethod of the sub process is called, it will register the signal handler automatically.
Examples
More examples in [examples](https://github.com/huyanping/simple-fork-php/tree/master/examples examples) dictionary
A simple example.
class TestRunnable implements \Jenner\SimpleFork\Runnable{ /** * Entrance * @return mixed */ public function run() { echo "I am a sub process" . PHP_EOL; } } $process = new \Jenner\SimpleFork\Process(new TestRunnable()); $process->start(); $process->wait();
A process using callback
$process = new \Jenner\SimpleFork\Process(function(){ for($i=0; $i<3; $i++){ echo $i . PHP_EOL; sleep(1); } }); $process->start(); $process->wait();
Process communication using shared memory
class Producer extends \Jenner\SimpleFork\Process{ public function run(){ $cache = new \Jenner\SimpleFork\Cache\SharedMemory(); //$cache = new \Jenner\SimpleFork\Cache\RedisCache(); for($i = 0; $i<10; $i++){ $cache->set($i, $i); echo "set {$i} : {$i}" . PHH_EOL; } } } class Worker extends \Jenner\SimpleFork\Process{ public function run(){ sleep(5); $cache = new \Jenner\SimpleFork\Cache\SharedMemory(); //$cache = new \Jenner\SimpleFork\Cache\RedisCache(); for($i=0; $i<10; $i++){ echo "get {$i} : " . $cache->get($i) . PHP_EOL; } } } $producer = new Producer(); $worker = new Worker(); $pool = new \Jenner\SimpleFork\Pool(); $pool->execute($producer); $pool->execute($worker); $pool->wait();
Process communication using system v message queue
class Producer extends \Jenner\SimpleFork\Process { public function run() { $queue = new \Jenner\SimpleFork\Queue\SystemVMessageQueue(); //$queue = new \Jenner\SimpleFork\Queue\RedisQueue(); for ($i = 0; $i < 10; $i++) { echo getmypid() . PHP_EOL; $queue->put($i); } } } class Worker extends \Jenner\SimpleFork\Process { public function run() { sleep(5); $queue = new \Jenner\SimpleFork\Queue\SystemVMessageQueue(); //$queue = new \Jenner\SimpleFork\Queue\RedisQueue(); for ($i = 0; $i < 10; $i++) { $res = $queue->get(); echo getmypid() . ' = ' . $i . PHP_EOL; var_dump($res); } } } $producer = new Producer(); $worker = new Worker(); $pool = new \Jenner\SimpleFork\Pool(); $pool->execute($producer); $pool->execute($worker); $pool->wait();
Process communication using Semaphore lock
class TestRunnable implements \Jenner\SimpleFork\Runnable { /** * @var \Jenner\SimpleFork\Lock\LockInterface */ protected $sem; public function __construct() { $this->sem = \Jenner\SimpleFork\Lock\Semaphore::create("test"); //$this->sem = \Jenner\SimpleFork\Lock\FileLock::create("/tmp/test.lock"); } /** * @return mixed */ public function run() { for ($i = 0; $i < 20; $i++) { $this->sem->acquire(); echo "my turn: {$i} " . getmypid() . PHP_EOL; $this->sem->release(); sleep(1); } } } $pool = new \Jenner\SimpleFork\Pool(); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->wait();
Process pool to manage processes
$pool = new \Jenner\SimpleFork\Pool(); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->wait();
ParallelPool to manage processes
$fixed_pool = new \Jenner\SimpleFork\ParallelPool(new TestRunnable(), 10); $fixed_pool->start(); $fixed_pool->keep(true);
FixedPool to manage processes
$pool = new \Jenner\SimpleFork\FixedPool(2); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->wait();
SinglePool to manage processes
$pool = new \Jenner\SimpleFork\SinglePool(); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->execute(new \Jenner\SimpleFork\Process(new TestRunnable())); $pool->wait();
jenner/simple_fork 适用场景与选型建议
jenner/simple_fork 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 308.02k 次下载、GitHub Stars 达 227, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「fork」 「process manager」 「fork manager」 「process ipc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jenner/simple_fork 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jenner/simple_fork 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jenner/simple_fork 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
PHP Trello API Wrapper (fork for php7+) from https://github.com/ashwinks/Trello-API-PHP-Wrapper
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Shell command module for PHP.
A fluent PHP library for supervised master-child process control using pcntl and pipes
Swoole server process management
统计信息
- 总下载量: 308.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 233
- 点击次数: 26
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04