lucinda/process
Composer 安装命令:
composer require lucinda/process
包简介
Light weight API performing single/multiple shell command execution using IO multiplexing
README 文档
README
This API is a light weight wrapper over PHP proc_* functions, able to execute shell processes individually, pool them or handle streams that traverse them. Its design goals were:
- platform agnosticity: API doesn't take any assumptions on the operating system process runs into
- least assumption principle: API doesn't take any assumption how you will handle process streams, so provides a skeleton instead
- elegance and simplicity: API is written on less is more principle so it's easy to understand and flexible to extend
API is 100% unit tested, fully PSR-4 compliant and only requiring PHP 8.1+ interpreter. For installation, you just need to write this in console:
composer require lucinda/process
Then use one of main classes provided (using use Lucinda\Shell namespace):
- Stream: encapsulates an abstract data stream to be used by process (eg: STDIN/STDOUT/STDER). Extended by:
- Stream\Pipe: encapsulates a stream of un-named pipes to be processed immediately
- Stream\File: encapsulates a stream of that delegates to a file on disk
- Stream\Resource: encapsulates a stream of that delegates to a resource (eg: socket)
- Process: encapsulates a single process using Stream instances above
- Pool: encapsulates a pool of processes to be executed in paralel queue-ing Process instances above and using Process\Multiplexer implementation for processing
Each of above classes branches through its methods to deeper classes that become relevant depending on the complexity of process execution logic. To make things simple following drivers were provided (using Lucinda\Shell\Driver namespace):
- SingleCommandRunner: executes a single Process and returns Process\Result
- MultiCommandRunner: executes list of Process-es and returns list of Process\Result-s
Both classes make use of I/O multiplexing that uses SELECT command underneath.
Executing Single Processes
To execute a single shell command you can use Lucinda\Shell\Process for minute control or Lucinda\Shell\Driver\SingleCommandRunner driver provided:
use Lucinda\Shell\Driver\SingleCommandRunner; use Lucinda\Shell\Process; $object = new SingleCommandRunner(5); $result = $object->run(new Process("YOUR_SHELL_COMMAND"));
This is superior to shell_exec because it will:
- terminate if shell command execution exceeds 5 seconds
- process STDOUT/STDERR streams separately using IO multiplexing
- read streams in parallel using chunks
- automatically escape shell command
Executing Multiple Processes
To execute multiple shell commands at once you can implement your own Lucinda\Shell\Process\Multiplexer for minute control or Lucinda\Shell\Driver\MultiCommandRunner driver provided:
use Lucinda\Shell\Driver\MultiCommandRunner; use Lucinda\Shell\Process; $object = new MultiCommandRunner(5); $results = $object->run([ new Process("YOUR_SHELL_COMMAND1"), new Process("YOUR_SHELL_COMMAND2"), ... ]);
This will:
- open processes simultaneously
- terminate if ANY of processes exceeds 5 seconds
- use non-blocking approach, allowing streams processing to be done in parallel
- automatically escape shell commands
Pooling Multiplexed Processes
To run multiplexed processes in pools of fixed size (5 in this example):
use Lucinda\Shell\Driver\MultiCommandRunner; use Lucinda\Shell\Process; use Lucinda\Shell\Pool; # defines a pool of max 5 capacity $pool = new Pool(5); # adds processes to pool $pool->submit(new Process("YOUR_SHELL_COMMAND1")); $pool->submit(new Process("YOUR_SHELL_COMMAND2")); ... # executes processes in batches, delegating to Multiplexer instances $results = $pool->shutdown(new MultiCommandRunner(5));
This will:
- execute N processes at same time (5 in above example)
- when they all end, proceed to next batch
lucinda/process 适用场景与选型建议
lucinda/process 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「process」 「Streams」 「pool」 「stdout」 「stdin」 「stderr」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lucinda/process 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lucinda/process 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lucinda/process 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
Pop Http Component for Pop PHP Framework
Shell command module for PHP.
Swoole server process management
PHP streams library
A concurrent GuzzleHttp Request Pool that actually works
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-28