seankndy/daemon
Composer 安装命令:
composer require seankndy/daemon
包简介
PHP package for creating and managing daemons
README 文档
README
$ composer require seankndy/daemon
Usage overview
On each iteration of the main loop, producers are called which should provide a task to fork/run.
Producers can be callables or objects that implement SeanKndy\Daemon\Tasks\Producer. Producers should return null
if there is no task to perform otherwise a SeanKndy\Daemon\Tasks\Task or callable. SeanKndy\Daemon\Tasks\Task
has 3 methods:
init() - Called from parent process just before child is forked.
run() - Called from within forked process (child work)
finish(int $status) - Called from parent when process exits
So if you need to perform any initialization or teardown of as children are spawned/exiting, then
you'll want to make your own task classes implementing SeanKndy\Daemon\Tasks\Task rather than using
a simple closure.
There are various events you can listen for (use SeanKndy\Daemon\Daemon::addListener()):
SeanKndy\Daemon\DaemonEvent::START - When daemon starts
SeanKndy\Daemon\DaemonEvent::STOP - When daemon stops
SeanKndy\Daemon\DaemonEvent::DAEMONIZED - When daemonized (backgrounded)
SeanKndy\Daemon\DaemonEvent::LOOP_ITERATION - Called at the end of each loop iteration
SeanKndy\Daemon\Processes\Event::START - New process started
SeanKndy\Daemon\Processes\Event::EXIT - New process exited
SeanKndy\Daemon\Processes\Event::ITERATION - Every main loop iteration this is fired for each running process
<?php use SeanKndy\Daemon\Daemon; use SeanKndy\Daemon\Processes\Event as ProcessEvent; $maxProcesses = 50; $quietTime = 1000000; $childTimeout = 30; $daemon = new Daemon($maxProcesses, $quietTime, $childTimeout); // $producer can be a callable or an object that implements SeanKndy\Daemon\Tasks\Producer // it should produce work to do (which is also a callable or an object implementing SeanKndy\Daemon\Tasks\Task) // // if multiple producers are added, they will be round-robined $number = 0; $producer = function() use (&$number) { if ($number >= 10) return null; $number++; // this is the "task" or the code to run within the forked child return function() use ($number) { echo "hello from child $number, my pid is " . getmypid() . "\n"; return 0; // child exit value }; }; $daemon->addProducer($producer); // optional, signal catching $daemon->addSignal(SIGTERM, function ($signo) { // SIGTERM caught, handle it here }); // optional, example event listeners $daemon->addListener(ProcessEvent::START, function ($event) { // process $event->getProcess() started }); $daemon->addListener(ProcessEvent::EXIT, function ($event) { // process $event->getProcess() exited }); $daemon->setDaemonize(false); // don't fork to background $daemon->start();
Inter-process Communication example
You can use \SeanKndy\Daemon\IPC\Socket to send message from child to parent using socket pairs.
Here is an example of doing that:
<?php use SeanKndy\Daemon\Tasks\Task; class MyTask implements Task { private $ipc; /** * Initialize task (main thread) * * @return void */ public function init() : void { $this->ipc = new \SeanKndy\Daemon\IPC\Socket(); } /** * Do the work (child thread) * * @return int */ public function run() : int { // do something useful in child and generate $interestingData $this->ipc->send($interestingData); $this->ipc->close(); } /** * Cleanup (main thread) * * @var int $status Exit status of child thread * @return void */ public function finish(int $status) : void { if ($this->ipc->hasMessage()) { $interestingData = $this->ipc->receive(); // $interestingData now in parent process } $this->ipc->close(); } }
seankndy/daemon 适用场景与选型建议
seankndy/daemon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「process」 「fork」 「daemon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 seankndy/daemon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 seankndy/daemon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 seankndy/daemon 相关的其它包
同方向 / 同关键字的高下载量 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
Shell command module for PHP.
A fluent PHP library for supervised master-child process control using pcntl and pipes
Swoole server process management
Generic PHP Threads library using only pure PHP
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-05-27