Guzzle:使用 Guzzle 的 Pool:batch() 和 `sink` 选项进行并行文件下载

Posted

技术标签:

【中文标题】Guzzle:使用 Guzzle 的 Pool:batch() 和 `sink` 选项进行并行文件下载【英文标题】:Guzzle: Parallel file download using Guzzle's Pool:batch() and `sink` option 【发布时间】:2015-10-29 16:23:57 【问题描述】:

您可以使用 Guzzle 的 Pool:batch() 方法并行执行 http 请求。它允许您使用第三个参数中的options 键为请求设置默认选项。

但是,如果我需要针对池中的不同请求使用不同的选项怎么办?我想使用池执行 GET 请求并将每个响应流式传输到磁盘上的不同文件。有一个sink 选项。但是如何将此选项的不同值应用于请求?

【问题讨论】:

【参考方案1】:

Rastor's example几乎是正确的,但是如果您想为Pool() 构造函数提供“选项”,如果执行不正确。

他缺少提到的池选项数组的关键实现here。

Guzzle 文档说:

当迭代器产生一个函数时,该函数被提供 应该合并在任何顶部的“request_options”数组 现有选项,然后该函数必须返回一个可等待 承诺。

此外,如果您查看我链接到的评论下方的 Pool() 代码,您会看到 Guzzle 的 Pool 调用了可调用对象并将 Pool 的“选项”作为参数提供给它,这样您就应该应用根据您的要求。

正确的优先级是

每个请求的选项 > 池选项 > 客户端默认值。

如果您Pool() 对象的选项数组应用于您的请求对象,您最终会遇到严重的错误,例如如果您尝试创建new Pool($client, $requests(100), ['options'=>['timeout'=>30.0]]);。如果没有我更正的代码,您的 Pool-options 将根本不会应用,因为 不支持正确合并 pool options,因此最终放弃了它们。

所以这里是支持Pool()选项的正确代码:

<?php

$client = new \GuzzleHttp\Client();

$requests = function ($total) use ($client) 
    for ($i = 0; $i < $total; $i++) 
        $url = "domain.com/picture/$i.jpg";
        $filepath = "/tmp/$i.jpg";

        yield function($poolOpts) use ($client, $url, $filepath) 
            /** Apply options as follows:
             * Client() defaults are given the lowest priority
             * (they're used for any values you don't specify on
             * the request or the pool). The Pool() "options"
             * override the Client defaults. And the per-request
             * options ($reqOpts) override everything (both the
             * Pool and the Client defaults).
             * In short: Per-Request > Pool Defaults > Client Defaults.
             */
            $reqOpts = [
                'sink' => $filepath
            ];
            if (is_array($poolOpts) && count($poolOpts) > 0) 
                $reqOpts = array_merge($poolOpts, $reqOpts); // req > pool
            

            return $client->getAsync($url, $reqOpts);
        ;
    
;

$pool = new Pool($client, $requests(100));

但是请注意,您不必支持Pool() 选项,如果您知道您永远不会向new Pool() 构造函数添加任何选项。在这种情况下,您可以查看the official Guzzle docs 的示例。

官方示例如下:

// Using a closure that will return a promise once the pool calls the closure.
$client = new Client();

$requests = function ($total) use ($client) 
    $uri = '127.0.0.1:8126/guzzle-server/perf';
    for ($i = 0; $i < $total; $i++) 
        yield function() use ($client, $uri) 
            return $client->getAsync($uri);
        ;
    
;

$pool = new Pool($client, $requests(100));

【讨论】:

在这种情况下如何取回正确的索引?假设您使用数组而不是 $total $a['a'] = 'url', $a['x'] = 'url' 等等。你怎么知道哪个请求结果属于 $a 的哪个数组键?【参考方案2】:

对于大嘴 6

$client = new \GuzzleHttp\Client();

$requests = function ($total) use ($client) 
    for ($i = 0; $i < $total; $i++) 
        $url = "http://domain.com/picture/$i.jpg";
        $filepath = "/tmp/$i.jpg";

        yield function() use ($client, $url, $filepath) 
            return $client->getAsync($url, [
                'sink' => $filepath
            ]);
        ;
    
;

$pool = new Pool($client, $requests(100));

【讨论】:

【参考方案3】:

您可以在请求中单独指定您想要的$options。如果您将其传递给客户端,它将仅适用于所有请求。这是 Guzzle 6 文档的摘录:

创建客户端时,可以将标头添加为默认选项。什么时候 标头用作默认选项,它们仅适用于 正在创建的请求尚未包含特定的标头。 这包括在 send() 中传递给客户端的请求和 客户端创建的 sendAsync() 方法和请求(例如, request() 和 requestAsync())。

见http://guzzle.readthedocs.org/en/latest/request-options.html?highlight=default#headers

【讨论】:

它同时适用于所有请求。我需要为每个请求单独的 $options 以将每个响应正文下载到单独的文件中。 如果你把 $options 数组放在 Request 对象上,它只会适用于那个请求...我改写了我的句子 问题是关于使用池,而不是单独的 Request 对象;) 无论如何,您都必须创建您的请求对象...并将它们传递给池。我在寻找我的问题的答案时发现了你的问题,检查我如何创建一批请求并将它们传递给批处理方法:***.com/questions/32323140/…

以上是关于Guzzle:使用 Guzzle 的 Pool:batch() 和 `sink` 选项进行并行文件下载的主要内容,如果未能解决你的问题,请参考以下文章

PHP 使用 Guzzle 执行 HTTP 请求

PHP 使用 Guzzle 执行 HTTP 请求

如何使用 Guzzle 进行 HTTP 基本身份验证?

Prestashop Guzzle 冲突

使用 Guzzle 捕获 CUrl 超时异常

在 Guzzle 中设置代理