kore/njq
Composer 安装命令:
composer require kore/njq
包简介
Native PHP Job Queue
README 文档
README
To make use of multiple cores for some rather long processing operations I needed a way to fork multiple workers from a single PHP script multiple times lately. So I created this small project which implements this in a way, so that it should reusable by anybody.
Native job queue
The implementation and usage is pretty simple. There is in an interface
JobProvider which has the methods hasJobs() and getNextJob(), which
you need to implement. The second method should return valid PHP callbacks
- for example Closures. Those are then executed (in parallel) by the executor.
I also implemented a ShellJobProvider (which implements JobProvider),
which is constructed from an array of shell commands, which then are executed
in parallel. A simple working example:
<?php
require 'njq/environment.php';
$executor = new \njq\Executor();
$executor->run(
new \njq\ShellJobProvider( array(
'echo 1 >> test',
'echo 2 >> test',
'echo 3 >> test',
'echo 4 >> test',
'echo 5 >> test',
) ),
4
);
?>
The file test will then contain something like (the order might vary):
5 4 3 2 1
The 4 (second parameter of \njq\Executor::run) defines the number of
parallel processes to spawn. This should not exceed the number of available
cores in the most cases.
Logger
To view the state of the executor you can specify a logger, which then can echo
the current progress. A logger needs to implement the \njq\Logger interface
and is passed to the constructor of the executor. If no logger is specified a
blind dummy logger will be used. To use the shell logger, echoing the state to
STDERR, use:
<?php
require 'njq/environment.php';
$executor = new \njq\Executor( new \njq\ShellLogger() );
$executor->run(
new \njq\ShellJobProvider( array(
// ...
) ),
4
);
It will then print a status like the following, if the JobProvider
implements the Countable interface:
56 / 5880 (0.95%) |
If the JobProvider does not implement the Countable interface the
percent indicator obviously cannot be displayed.
Requirements
The job queue requires PHP 5.3 and the PHP PCNTL extension.
kore/njq 适用场景与选型建议
kore/njq 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 319 次下载、GitHub Stars 达 53, 最近一次更新时间为 2023 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kore/njq 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kore/njq 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 319
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 53
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-17