本文经受权转自 PHPHub 社区php
Guzzle 库是一套强大的 PHP HTTP 请求套件。html
本文重点演示如何使用 Guzzle 发起多线程请求。laravel
Guzzle 并发请求文档github
php artisan make:console MultithreadingRequest --command=test:multithreading-request
编辑 app/Console/Kernel.php
,在 $commands 数组中增长:api
Commands\MultithreadingRequest::class,
修改 app/Console/Commands/MultithreadingRequest.php
文件,在 handle
方法中增长:数组
$this->info('hello');
输出:promise
$ php artisan test:multithreading-request hello
composer require guzzlehttp/guzzle "6.2"
一份可运行的代码赛过千言万语呀。多线程
下面代码是 app/Console/Commands/MultithreadingRequest.php
里的内容:
<?php namespace App\Console\Commands; use GuzzleHttp\Client; use GuzzleHttp\Pool; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Exception\ClientException; use Illuminate\Console\Command; class MultithreadingRequest extends Command { private $totalPageCount; private $counter = 1; private $concurrency = 7; // 同时并发抓取 private $users = ['CycloneAxe', 'appleboy', 'Aufree', 'lifesign', 'overtrue', 'zhengjinghua', 'NauxLiu']; protected $signature = 'test:multithreading-request'; protected $description = 'Command description'; public function __construct() { parent::__construct(); } public function handle() { $this->totalPageCount = count($this->users); $client = new Client(); $requests = function ($total) use ($client) { foreach ($this->users as $key => $user) { $uri = 'https://api.github.com/users/' . $user; yield function() use ($client, $uri) { return $client->getAsync($uri); }; } }; $pool = new Pool($client, $requests($this->totalPageCount), [ 'concurrency' => $this->concurrency, 'fulfilled' => function ($response, $index){ $res = json_decode($response->getBody()->getContents()); $this->info("请求第 $index 个请求,用户 " . $this->users[$index] . " 的 Github ID 为:" .$res->id); $this->countedAndCheckEnded(); }, 'rejected' => function ($reason, $index){ $this->error("rejected" ); $this->error("rejected reason: " . $reason ); $this->countedAndCheckEnded(); }, ]); // 开始发送请求 $promise = $pool->promise(); $promise->wait(); } public function countedAndCheckEnded() { if ($this->counter < $this->totalPageCount){ $this->counter++; return; } $this->info("请求结束!"); } }
运行结果:
$ php artisan test:multithreading-request 请求第 5 个请求,用户 zhengjinghua 的 Github ID 为:3413430 请求第 6 个请求,用户 NauxLiu 的 Github ID 为:9570112 请求第 0 个请求,用户 CycloneAxe 的 Github ID 为:6268176 请求第 1 个请求,用户 appleboy 的 Github ID 为:21979 请求第 2 个请求,用户 Aufree 的 Github ID 为:5310542 请求第 3 个请求,用户 lifesign 的 Github ID 为:2189610 请求第 4 个请求,用户 overtrue 的 Github ID 为:1472352 请求结束!
注意请求是同时发送过去的,由于 concurrency
并发设置了 7,因此 7 个请求同时发送,只不过接收到返回的时间点不同。
完。