bentools / guzzle-queued
异步请求队列,由工作者处理。
v2.7
2016-10-03 13:09 UTC
Requires
- guzzlehttp/guzzle: ~6.0
- pda/pheanstalk: ^3.1
- symfony/console: ~2.3|~3.0
- symfony/event-dispatcher: ~2.3|~3.0
README
此库允许您通过Guzzle 6发送异步请求,使用队列系统(目前仅支持Beanstalk)。
安装
composer require bentools/guzzle-queued
使用方法
use BenTools\GuzzleQueued\Client as QueuedClient; use GuzzleHttp\Psr7\Request as PSR7Request; use Pheanstalk\Pheanstalk; require_once __DIR__ . '/../vendor/autoload.php'; $pheanstalk = new Pheanstalk('127.0.0.1'); $guzzle = new \GuzzleHttp\Client(); $client = new QueuedClient($guzzle, $pheanstalk); $request = new PSR7Request('GET', 'http://httpbin.org/user-agent'); $promise = $client->sendAsync($request)->then(function (\Psr\Http\Message\ResponseInterface $response) { echo (string) $response->getBody(); }); $promise->wait(); // Now the hard work has to be done by a separate PHP process
使用以下命令启动所需数量的工作者
php vendor/bin/guzzle-request-worker.php &
事件分发器
创建自己的工作者并将其连接到以下事件
- \BenTools\GuzzleQueued\JobEvent::BEFORE_PROCESS:在请求处理之前
- \BenTools\GuzzleQueued\JobEvent::AFTER_PROCESS:请求处理之后
- \BenTools\GuzzleQueued\JobEvent::ERROR:如果发生错误
更改响应、阻止请求有效发送、延迟请求等...