定制 fastbill/parallel-process-dispatcher 二次开发

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

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

fastbill/parallel-process-dispatcher

Composer 安装命令:

composer require fastbill/parallel-process-dispatcher

包简介

Tiny PHP library for running jobs in background and/or in parallel

README 文档

README

This micro-library has two classes. One encapsulates a (linux commandline) process into an object and allows asynchronous running without deadlocks. The other is a multi-process-dispatcher which takes an arbitrary number of beforementioned processes and runs them simultaneously (with a maximum number of concurrent processes).

Usage examples:

  • Dispatching long running cronjobs which e.g. mostly wait for webservice responses (you can run more processes than max. CPUs)
  • Running background workers which listen on a queue (maximum should be number of CPUs)
  • Running commandline-tasks inside a web application simultaneously, e.g. PDF-Generation, Image-Processing etc.

Installation:

Add the following to your composer.json:

{
    "require": {
        "fastbill/parallel-process-dispatcher": "*"
    }
}

or just run the following command in your project root directory

$ composer require "fastbill/parallel-process-dispatcher"

Usage

Classes

Process

$process = new Process('pngcrush --brute background.png');
$process->start();

// optional: do something else in your application

while (! $process->isFinished() ) {
    usleep(1000); //wait 1ms until next poll
}
echo $process->getOutput();

Dispatcher

$process1 = new Process('pngcrush --brute background.png');
$process2 = new Process('pngcrush --brute welcome.png'); 
$process3 = new Process('pngcrush --brute logo.png'); 

$dispatcher = new Dispatcher(2);    // will make sure only two of those will actually run at the same time
$dispatcher->addProcess($process1);
$dispatcher->addProcess($process2);
$dispatcher->addProcess($process3);

$dispatcher->dispatch();  // this will run until all processes are finished.

$processes = $dispatcher->getFinishedProcesses();

foreach ($processes as $process) {
    echo $process->getOutput(), "\n\n";
}

Advanced

Using Process and Dispatcher to start multiple processes and later collect the results

$dispatcher = new Dispatcher(2);

$process1 = new Process('pngcrush --brute background.png');
$dispatcher->addProcess($process1, true);   // true starts the process if there are still free slots

// [... more code ...]

$process2 = new Process('pngcrush --brute welcome.png'); 
$dispatcher->addProcess($process2, true);

// [... more code ...]

// during code execution, the dispatcher cannot remove finished processes from the stack, so you have to call the tick()-function
// if you want the queue to advance - but it's optional since at latest the __destruct() function will call dispatch(); 
$dispatcher->tick();

// [... more code ...]

$dispatcher->dispatch();  // this will make the dispatcher wait until all the processes are finished, if they are still running

$processes = $dispatcher->getFinishedProcesses();

// loop over results

Known Issues

Process

  • PHP Internals: Be aware that if the child process produces output, it will write into a buffer until the buffer is full. If the buffer is full the child pauses until the parent reads from the buffer and makes more room. This is done in the isFinished() method. The dispatcher calls this method periodically to prevent a deadlock. If you use the process class standalone, you have to possibilities to prevent this:
    • call isFinished() yourself in either a loop, using a tick function or otherwise during execution of your script
    • instead of writing to stdOut, divert output to a temporary file and use its name as output.

Dispatcher

  • Multiple dispatchers (in different processes) are not aware of each other. So if you have a script that uses a dispatcher to call another script which itself uses a dispatcher to spawn multiple processes, you will end up with more child processes than the maximum, so choose the maximum accordingly or use a queue (e.g. Redis) and make the workers aware of each other by e.g. registering in a redis-stack for running workers.

fastbill/parallel-process-dispatcher 适用场景与选型建议

fastbill/parallel-process-dispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.51k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 fastbill/parallel-process-dispatcher 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 15.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 4
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-05-10