yidas/worker-dispatcher
Composer 安装命令:
composer require yidas/worker-dispatcher
包简介
PHP multi-processing task dispatcher with managing workers
README 文档
README
PHP Worker Dispatcher
PHP multi-processing task dispatcher with managing workers
Features
-
Multi-Processing implementation on native PHP-CLI
-
Tasks Dispatching to each worker process
-
Elegant Interface for setup and use
OUTLINE
DEMONSTRATION
Use multi-processing to dispatch tasks with generating workers based on CPU cores:
\yidas\WorkerDispatcher::run([ 'tasks' => ["R4NEJ1", "F5KH83", "..."], 'callbacks' => [ // The callback is for each forked process with decentralized tasks 'task' => function ($config, $workderId, $task) { // $task is one of the `tasks` assigned to each worker, ex. "F5KH83" for $workderId is 2 $token = $task; $result = file_get_contents("https://example/v1/register-by-token/{$token}"); }, ], ]);
Use multi-processing to digest jobs from queue:
\yidas\WorkerDispatcher::run([ 'tasks' => false, 'callbacks' => [ // The callback is for each forked process 'process' => function ($config, $workderId, $task) { // Get and handle each job from queue in inifite loop (You need to define your own function) while (true) { $result = handleOneJobFromQueue(); if ($result === null) { break; } } }, ], ]);
INTRODUCTION
This library is implemented by PHP PCNTL control, which provides a main PHP-CLI to fork multiple child processes to share tasks, and even can use for high concurrency application with infinite loop setting.
Since PHP has no shared variables or queue mechanism natively, if you don’t have an external job queue, this library provides a task average dispatcher to simply solve the core distributed processing problem.
REQUIREMENTS
This library requires the following:
- PHP PCNTL
- PHP CLI 5.4.0+
INSTALLATION
Run Composer in your project:
composer require yidas/worker-dispatcher ~1.0.0
Then you could use the class after Composer is loaded on your PHP project:
require __DIR__ . '/vendor/autoload.php'; use yidas\WorkerDispatcher;
USAGE
Calling the run() method statically with options as argument, WorkerDispatcher will start to dispatch tasks (if any), and then fork the number of workers according to the environment or settings, and wait for all forked processes to complete or terminate the main process.
The setting example with all options is as following:
\yidas\WorkerDispatcher::run([ 'debug' => true, 'workers' => 4, 'config' => ['uri' => "/v1/resource"], 'tasks' => ["R4NEJ1", "F5KH83", "..."], 'callbacks' => [ 'process' => function ($config, $workerId, $tasks) { echo "The number of tasks in forked process - {$workerId}: " . count($tasks[$workerId - 1]) . "\n"; }, 'task' => function ($config, $workerId, $task) { echo "Forked process - {$workerId}: Request to {$config['uri']} with token {$task}\n"; }, ], ]);
Options
| Option | Type | Deafult | Description |
|---|---|---|---|
| debug | boolean | false | Debug mode |
| workers | integer | (auto) | The number of workers(processes) to fork. (The default is the same as the number of CPU cores) |
| config | multitype | null | The custom variable used to bring in the callback function |
| tasks | multitype | array | For dispatching to each forked process. - Array: Each value of array will be dispatched to all forked processes. - Integer: The number of loops dispatched to all forked processes. - false: Perform finite loop. |
| callbacks.process | callable | nul | Callback function called after each forked process is created |
| callbacks.task | callable | nul | Callback function called in each task loop of each forked process |
callbacks.process
Callback function called after each forked process is created
function (multitype $config, integer $workerId, array $tasks)
| Argument | Type | Deafult | Description |
|---|---|---|---|
| $config | multitype | null | The custom variable used to bring in the callback function |
| $workerId | integer | (auto) | The sequence number of the worker(processes) in current function (Start from 1) |
| $tasks | multitype | array | Tasks array list for the worker(processes) in current function |
callbacks.task
Callback function called in each task loop of each forked process
function (multitype $config, integer $workerId, multitype $task)
| Argument | Type | Deafult | Description |
|---|---|---|---|
| $config | multitype | null | The custom variable used to bring in the callback function |
| $workerId | integer | (auto) | The sequence number of the worker(processes) in current function (Start from 1) |
| $tasks | multitype | array | The value of each tasks array list for each worker(processes) in current function |
yidas/worker-dispatcher 适用场景与选型建议
yidas/worker-dispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 898 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「task」 「worker」 「pcntl」 「multiprocessing」 「worker-manage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yidas/worker-dispatcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yidas/worker-dispatcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yidas/worker-dispatcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Queue
SlimQ Enables You to push jobs to background workers
Expose the DDev executable commands to the Robo task runner.
A lightweight task queue on top of rybakit/phive-queue
A replica of the AWS Elastic Beanstalk worker SQS daemon (sqsd) in PHP
Task system for APPUI
统计信息
- 总下载量: 898
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-21
