hdvianna/parallel-workerpool
Composer 安装命令:
composer require hdvianna/parallel-workerpool
包简介
README 文档
README
Parallel worker pool uses the PHP parallel extension to provide a simple interface for dealing with parallelization of tasks.
Usage
The WorkerPool requires an implementation of the WorkFactoryInterface
which is responsible for creating the consumer and producer closures.
A producer closure must return a Generator.
Composer installation
composer require hdvianna/parallel-workerpool
Runing with Docker
docker-compose up
Docker compose builds an environment with the needed extensions installed and create a bind mount to the current directory.
Example
In this example 10 workers will sleep for n milliseconds, each time they consume the work generated by the WorkFactory.
use hdvianna\Concurrent\WorkFactoryInterface; use hdvianna\Concurrent\WorkerPool; (new WorkerPool(new class implements WorkFactoryInterface { public function createWorkGeneratorClosure(): \Closure { return function () { for ($i = 0; $i < 100; $i++) { $work = new \stdClass(); $work->time = mt_rand(300, 1000); $work->id = $i; yield $work; } }; } public function createWorkConsumerClosure(): \Closure { return function($work) { printf("[$work->id]: Sleeping for %d milliseconds ...%s", $work->time, PHP_EOL); usleep($work->time * 1000); printf("[$work->id]: Woke up after %d milliseconds ...%s", $work->time, PHP_EOL); }; } }, 10))->run();
Synchronizing data
Data can be synchronized by using lock and unlock closures sent to the worker functions.
The shared data are received from the $lock closure and sent to the $unlock closure.
The last value sent can be get invoking the WorkerPool::lastValue()
use hdvianna\Concurrent\WorkFactoryInterface; use hdvianna\Concurrent\WorkerPool; $sharedData = 700; $works = 1000; $pool = new WorkerPool((new class ($sharedData, $works) implements WorkFactoryInterface { /** * @var int */ private $sharedData; /** * @var int */ private $works; /*** * constructor. * @param int $sharedData * @param int $works */ public function __construct($sharedData, $works) { $this->works = $works; $this->sharedData = $sharedData; } public function createWorkGeneratorClosure(): \Closure { $workers = $this->works; return function () use ($workers) { for ($i = 0; $i < $workers; $i++) { $work = new \stdClass(); $work->value = 1; yield $work; } }; } public function createWorkConsumerClosure(): \Closure { $initialValue = $this->sharedData; //Use the $lock and $unlock closures to synchronize data return function ($work, $lock, $unlock) use ($initialValue) { /*Synchronize the data. Will block and wait for data. $lock will return the last value*/ $shared = $lock(); if (!isset($shared)) { //Data was not initialized $shared = $initialValue; } $shared += $work->value; //Unlocks sending the new data. $unlock($shared); }; } }), 10); $pool->run(); //Get the last value sent to the unlock closure $result = $pool->lastValue(); echo("\$result equals to \$works + \$sharedData?" . PHP_EOL); echo("($result equals to $works + $sharedData?)" . PHP_EOL); echo(assert($result === ($works + $sharedData)) ? "Yes!": "No =(").PHP_EOL;
hdvianna/parallel-workerpool 适用场景与选型建议
hdvianna/parallel-workerpool 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hdvianna/parallel-workerpool 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hdvianna/parallel-workerpool 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-18