定制 mehr-als-nix/parallel 二次开发

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

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

mehr-als-nix/parallel

Composer 安装命令:

composer require mehr-als-nix/parallel

包简介

This package is a port of PhpDumentor/Parallel

README 文档

README

This is a library for introducing Parallelization into your project. See the example.php file for an example how to use this library.

Theory of Operation

This library will enable the developer to execute a given amount of tasks (workers) in parallel. This is achieved by adding workers onto a manager, optionally defining how many processes to run simultaneously and then execute the manager.

Under Linux this library will try to detect the number of processors and allow a maximum number of processes to run equal to the number of processors. If this cannot be determined or the user is running Windows then a default of 2 is used.

Requirements and graceful degradation

Parallelization has several requirements. But to allow distribution, without adding several requirements to your application, will this library execute the given tasks in serie if the requirements are not met. And throw a E_USER_NOTICE php error that explains to the user that dependencies are missing.

The requirements for this library are:

Workers

Workers are basically wrappers around callback functions or methods. As such you can use anything in your existing project and parallelize it.

Do note that each parallel process is a duplicate of the original. This means that, for example, if you pass an object (or other reference) and change that, that the changes that you have made do not carry over to the caller.

The return value of the given callback is stored as result on the worker and can be read using the getResult() method.

Any exception that is thrown will result in an error, where the getReturnCode() method will return the exception code (be warned: this may be 0!) and the getError() method will return the exception message.

Errors and exceptions

if a task throws an exception it is caught and registered as an error. The exception's code is used as error number, where the message is used as error message.

By using this, instead of dying, you can continue execution of the other parallel processes and handle errors yourself after all processes have been executed.

Examples

Fluent interface

use MehrAlsNix\Parallel\Manager;
use MehrAlsNix\Parallel\Worker;

$mgr = new Manager();
$mgr
  ->addWorker(new Worker(function() { sleep(1); return 'a'; }))
  ->addWorker(new Worker(function() { sleep(1); return 'b'; }))
  ->addWorker(new Worker(function() { sleep(1); return 'c'; }))
  ->addWorker(new Worker(function() { sleep(1); return 'd'; }))
  ->addWorker(new Worker(function() { sleep(1); return 'e'; }))
  ->execute();

/** @var Worker $worker */
foreach ($mgr as $worker) {
    var_dump($worker->getResult());
}

Array interface

use MehrAlsNix\Parallel\Manager;
use MehrAlsNix\Parallel\Worker;

$mgr = new Manager();
$mgr[] = new Worker(function() { sleep(1); return 'f'; });
$mgr[] = new Worker(function() { sleep(1); return 'g'; });
$mgr[] = new Worker(function() { sleep(1); return 'h'; });
$mgr[] = new Worker(function() { sleep(1); return 'i'; });
$mgr[] = new Worker(function() { sleep(1); return 'j'; });
$mgr->execute();

/** @var Worker $worker */
foreach ($mgr as $worker) {
    var_dump($worker->getResult());
}

TODO

  • Improve docs

  • More intelligent process slots; currently only the oldest in a 'set' of slots is waited on but if this runs for a longer time then the other slots than those will not be filled as long as the first slot is occupied.

  • Last parts of IPC (Inter-Process Communication), to be able to return information from Workers to the Manager.

    • STDOUT
    • STDERR

mehr-als-nix/parallel 适用场景与选型建议

mehr-als-nix/parallel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 839.99k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 mehr-als-nix/parallel 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 839.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-10