定制 liuggio/spawn 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

liuggio/spawn

Composer 安装命令:

composer require liuggio/spawn

包简介

Concurrent processing of closures and commands in PHP with ease.

README 文档

README

Build Status

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:

  1. executes and handles concurrent PHP closures.
  2. 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

  1. The callable is executed in a new isolated processes also with its "use" references.
  2. It's possible to add a listener for event handling.
  3. 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?

liuggio/spawn 适用场景与选型建议

liuggio/spawn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 29, 最近一次更新时间为 2015 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 liuggio/spawn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 liuggio/spawn 我们能提供哪些服务?
定制开发 / 二次开发

基于 liuggio/spawn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-01