liuggio/spawn
Composer 安装命令:
composer require liuggio/spawn
包简介
Concurrent processing of closures and commands in PHP with ease.
README 文档
README
The main job of Spawn is to improve the performance handling concurrent processes.
This library tries to do its best to speedup things using all the cores of the machine useful when you need to run bunch of commands eg: unit-tests/functional-tests/CS fixes/files handling
$spawn = new Spawn(); $spawn ->processes(range(1,10), "printenv > '/tmp/envs_{}{p}.log';") ->onCompleted(function(Process $singleProcess){ /* print stats */}); ->start();
Concurrent \Closure? Really?
Yes! With this library you can use concurrent closures,
BUT PHP is not Go-lang neither Erlang or any other famous language for concurrency,
and in order to simulate a isolated routine the closure is serialized and executed in a new PhpProcess,
be aware this is a workaround in order to speed up your concurrent Closures.
With this library you can also do:
- executes and handles concurrent PHP closures.
- spawns a single closure as an independent process.
Concurrent closures: Upload images to your CDN
Feed an iterator and it will break the job into multiple php scripts and spread them across many processes. In order to improve performances, the number of processes is equal to the number of computer's cores.
$spawn = new Spawn(); $files = new RecursiveDirectoryIterator('/path/to/images'); $files = new RecursiveIteratorIterator($files); $spawn->closures($files, function(SplFileInfo $file) { // upload this file }) ->start();
Each closure is executed in isolation using the PhpProcess component.
Spawn a single isolated closure
$spawn = new Spawn(); $sum = 3; $processes = $spawn ->spawn(["super", 120], function($prefix, $number) use ($sum) { echo $prefix." heavy routine"; return $number+$sum; }); // do something else here echo $processes->wait(); // 123 echo $processes->getOutput(); // "super heavy routine"
Advanced
- The callable is executed in a new isolated processes also with its "use" references.
- It's possible to add a listener for event handling.
- It's possible to get the return value of each callable, the ErrorOutput, the Output and other information.
$collaborator = new YourCollaborator(1,2,3,4); $spawn ->closures(range(1, 7), function($input) use ($collaborator) { echo "this is the echo"; $collaborator->doSomething(); $return = new \stdClass(); $return->name = "name"; return $return; }) ->onCompleted(function(ClosureProcess $process){ // do something with $returnValue = $processes->getReturnValue(); $output = $processes->getOutput(); $errorOutput = $processes->getErrorOutput(); $time = $processes->startAt(); $memory = $processes->getMemory(); $duration = $processes->getDuration(); }) ->start();
Events:
Listeners can be attached to closures and processes.
->onStarted(function(ClosureProcess|Process $process){}); ->onCompleted(function(ClosureProcess|Process $process){}); ->onSuccessful(function(ClosureProcess|Process $process){}); ->onEmptyIterator(function (){}); ->onPartialOutput(function(ClosureProcess|Process $process){}) ->onLoopCompleted(function ($exitCode, StopwatchEvent $event)
Other libs:
There are not so many libraries that handle concurrent processes. The best I found is about forking processes spork it features a great API and with no work-around but it needs several PHP extensions.
License:
MIT License see the License.
More fun?
- see how the travis.yml run test suite with spawn_tests.
- have fun with fastest
liuggio/spawn 适用场景与选型建议
liuggio/spawn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 29, 最近一次更新时间为 2015 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 liuggio/spawn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 liuggio/spawn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-01