drealecs/thread-worker
Composer 安装命令:
composer require drealecs/thread-worker
包简介
PHP multi-thread worker message based library
README 文档
README
PHP multi-thread-worker message/event library
Introduction
Thread-worker is a library that allows execution of tasks in parallel by multiple PHP processes on the same computer or on different computers.
The library has a lot of concepts borrowed from other languages.
Concepts
-
Task represents a function that should be executed asynchronously.
-
Queue is a queue of tasks. Tasks can be put into queue by a process and taken out for execution by another process.
-
Executor is an wrapper over a queue. Tasks will be passed to it in one PHP script and in another php script RemoteExecutor will be used to work on those tasks.
-
TaskResult or TaskException are the result of a Task.
Task, TaskResult and TaskException are the entities that are being serialized as "messages" and write to/read from the queue.
API
Task
A code that will be executed remotely cannot share variables or context with the calling code. When defining a function
that can be executed asynchronously it must be created as a Task by extending \ThreadWorker\Task and implementing method run():
class AddTask extends ThreadWorker\Task { public function run($a, $b) { returns $a + $b; } }
and after that, the task can be used in this way:
$task = new AddTask(3, 5); $result = $task();
and this will make $result equals 8.
Just like a function, a task can return a value or not.
Of course, the example above execute a task locally, synchronously. To execute it asynchronously we will need a Queue and an Executor.
Queue
To synchronize working tasks a queue concept is being used.
There is a queue, someone puts a task in the queue and there are workers that take it and run it.
Interface of a task queue:
-
public function queue($task, $captureResult); - called to queue a task for execution. There are Tasks that don't return a result and Tasks that returns a result. A task that does not returns a result is usually preferable because the calling code can do other things and get out of scope or even finish execution. To accomplish this
$captureResultmust befalsein which case the methods does not returns anything. If we need a execute multiple task remotely and join their result we might need to pass second parameter astrueandqueue()method will return a task identifier that can be used later to query and retrieve the task result. -
public function start() - called by the script that can execute a task. This is a blocking method and it blocks until there is a task in the queue. It returns a RemoteTask which is a container for the Task and it's TaskResult.
-
public function end($remoteTask) - called by the script that executed the task. It marks the task a being finished and it task is one that returns a response, it stores the TaskResult.
-
public function getResult($taskId) - usually called by the script that queued the task for execution. It can be called only one time and is blocking until the task finished to execute.
-
public function isQueued|isRunning|isFinished($taskId) - methods that can query the state of a task.
-
public function getQueueSize() and getRunningSize() - methods that can query the queue for it's current workflow capacity.
Currently there is only one implementation of Queue: \ThreadWorker\RedisQueue and there are plans for: AMQPQueue, MySQLQueue
Executor
Executor wraps a queue and provide a simpler interface to queue task and work on tasks and get task results.
There is a QueueExecutor that has 2 methods:
- void execute(Task $task) - adds the task to the queue
- QueuedTask submit(Task $task) - adds the task to the queue and returns a QueuedTask instance that can be used to query task status and retrieve the task result.
Let's look at an example:
$queue = new ThreadWorker\RedisQueue('example'); $executor = new ThreadWorker\QueueExecutor($queue); $task = new AddTask(3, 5); $queuedTask = $executor->submit($task); $result = $queuedTask->getResult()->getValue();
QueueExecutor is extended into RemoteExecutor that does the asynchronous running of the tasks. Worker's code would look like this:
$queue = new ThreadWorker\RedisQueue('example'); $worker = new ThreadWorker\RemoteExecutor($queue); $worker->work();
An instance of RemoteExecutor is passed as an extra parameter to the run() method of the task and can be use to queue more tasks.
drealecs/thread-worker 适用场景与选型建议
drealecs/thread-worker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 6, 最近一次更新时间为 2014 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「threads」 「worker」 「asynchronous」 「Thread」 「multithread」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 drealecs/thread-worker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drealecs/thread-worker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 drealecs/thread-worker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Queue
Asynchronous coroutines for PHP 7.
SlimQ Enables You to push jobs to background workers
Symfony ResqueBundle
This Bundle provides threaded comment functionality for Symfony applications
Generic PHP Threads library using only pure PHP
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-01-04