A-A+
GuzzleHttp new Pool获取body内容及带出结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | $allBranches = array(); $requests = function($projects) { foreach($projects as $project) { yield new Request('GET', "projects/".$project['id']."/repository/branches?per_page=100"); } }; $pool = new Pool($this - >client, $requests($projects), ['concurrency' = >5, 'fulfilled' = > function($response, $index) use($allBranches) { $data = json_decode($response - >getBody() - >getContents(), true); print_r($data); array_push($allBranches, $data); //return $data; }, 'rejected' = > function($reason, $index) { // this is delivered each failed request echo "rejected"; }, ]); // Initiate the transfers and create a promise $promise = $pool - >promise(); // Force the pool of requests to complete. $response = $promise - >wait(); return $allBranches; |
这是一段代码,但是无$allBranches 返回结果。
大神回答:$allBranches
is passed by value into your'fulfilled'
closure. You could pass it by reference (e.g.,'fulfilled' => function ($response, $index) use (&$allBranches) {
instead.
There's a static batch
method on the Pool class that does something very similar to your example -- it pushes the results of pooled requests onto an array and then returns that array. To use that you would essentially just replace everything from $pool = new Pool(...
on down with return Pool::batch($this->client, $requests($projects), ['concurrency' => 5])
.
可以使用 batch 方法。
可参考:https://github.com/guzzle/guzzle/blob/master/src/Pool.php#L94
问题点出在红色标识的地方。
文章来源:https://github.com/guzzle/guzzle/issues/1155
布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏