dan-rogers/workerpool
Composer 安装命令:
composer require dan-rogers/workerpool
包简介
A very basic implementation of a worker pool in PHP for shell applications
README 文档
README
A simplistic approach to a threadpool in PHP
Installation
composer require dan-rogers/workerpool
Example use case
This implementation is intended for something like the following:
- You have many accounts to process, doing things which take a long time like multiple calls to a slow external API
- Running this in sequence would take too long so you need multiple threads
- The action you are taking is run in a command line script which take arguments
Example implementation
Lets use our above use case. I have 200 user accounts. For each user account I need to pull and save some from an API, process it and dispatch the processed result to another service.
Code for the above process. Lets call the script child-process.php:
<?php // Get info by ID $client = new HttpClient(); $info = $client->getInfo($argv[1]); // Run some processing $processed = $this->processInfo($info); // Dispatch to service $client->post($processed);
Invoking the above would be as simple as running php child-process.php {account_id}. In a main script lets use the Threadpool:
<?php use Psr\Log\NullLogger; use WorkerPool\WorkerPool; $account_ids = [1, 2, 3, 4 ... 199, 200]; $base_command = 'php child-process.php'; // The child process we want to execute. $logger = new NullLogger(); // A PSR 3 compliant logger to be used for monitoring and logging. $max_threads = 10; // The max number of threads the workerpool can utilise at once. $thread_check_sleep = 3000; // When all threads are in use how long should the pool wait before checking for an available thread. $max_event_exec_time = 60000; // The maximum amount of time the thread should live for. $check_limit = 1000; // How many checks should the pool make for a free thread before exiting. $output_destination = 'mylog.log'; // Where the output of the child should be directed. $pool = new WorkerPool( $base_command, $logger, $max_threads, $thread_check_sleep, $max_event_exec_time, $check_limit, $output_destination ); foreach ($account_ids as $id) { // You can alternatively use WorkerPool::setJobs() to set all at once $pool->pushJob[$id]); } $pool->execute();
dan-rogers/workerpool 适用场景与选型建议
dan-rogers/workerpool 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 7, 最近一次更新时间为 2024 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dan-rogers/workerpool 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dan-rogers/workerpool 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-12-01