- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
是否可以让 Guzzle 池等待请求?
现在我可以动态地向池中添加请求,但是一旦池为空,guzzle 就会停止(很明显)。
当我同时处理 10 个左右的页面时,这是一个问题,因为在处理生成的 HTML 页面并添加新链接之前,我的请求数组将为空。
这是我的发电机:
$generator = function () {
while ($request = array_shift($this->requests)) {
if (isset($request['page'])) {
$key = 'page_' . $request['page'];
} else {
$key = 'listing_' . $request['listing'];
}
yield $key => new Request('GET', $request['url']);
}
echo "Exiting...\n";
flush();
};
还有我的游泳池:
$pool = new Pool($this->client, $generator(), [
'concurrency' => function() {
return max(1, min(count($this->requests), 2));
},
'fulfilled' => function ($response, $index) {
// new requests may be added to the $this->requests array here
}
//...
]);
$promise = $pool->promise();
$promise->wait();
@Alexey Shockov 回答后编辑代码:
$generator = function() use ($headers) {
while ($request = array_shift($this->requests)) {
echo 'Requesting ' . $request['id'] . ': ' . $request['url'] . "\r\n";
$r = new Request('GET', $request['url'], $headers);
yield 'id_' . $request['id'] => $this->client->sendAsync($r)->then(function($response, $index) {
echo 'In promise fulfillment ' . $index . "\r\n";
}, function($reason, $index) {
echo 'in rejected: ' . $index . "\r\n";
});
}
};
$promise = \GuzzleHttp\Promise\each_limit($generator(), 10, function() {
echo 'fullfilled' . "\r\n";
flush();
}, function($err) {
echo 'rejected' . "\r\n";
echo $err->getMessage();
flush();
});
$promise->wait();
最佳答案
不幸的是,您不能使用生成器做到这一点,只能使用自定义迭代器。
我准备了 a gist with the full example ,但主要思想只是创建一个迭代器,它会以两种方式改变其状态(它可以在结束后再次生效)。
psysh 中的 ArrayIterator 示例:
>>> $a = new ArrayIterator([1, 2])
=> ArrayIterator {#186
+0: 1,
+1: 2,
}
>>> $a->current()
=> 1
>>> $a->next()
=> null
>>> $a->current()
=> 2
>>> $a->next()
=> null
>>> $a->valid()
=> false
>>> $a[] = 2
=> 2
>>> $a->valid()
=> true
>>> $a->current()
=> 2
考虑到这个想法,我们可以将这样的动态迭代器传递给 Guzzle 并让它完成工作:
// MapIterator mainly needed for readability.
$generator = new MapIterator(
// Initial data. This object will be always passed as the second parameter to the callback below
new \ArrayIterator(['http://google.com']),
function ($request, $array) use ($httpClient, $next) {
return $httpClient->requestAsync('GET', $request)
->then(function (Response $response) use ($request, $array, $next) {
// The status code for example.
echo $request . ': ' . $response->getStatusCode() . PHP_EOL;
// New requests.
$array->append($next->shift());
$array->append($next->shift());
});
}
);
// The "magic".
$generator = new ExpectingIterator($generator);
// And the concurrent runner.
$promise = \GuzzleHttp\Promise\each_limit($generator, 5);
$promise->wait();
正如我之前所说,完整示例在 the gist 中,与 MapIterator
和 ExpectingIterator
。
关于php - Guzzle 池 : Wait for Requests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754389/
当我运行以下代码(使用最新的 Guzzle v6)时,请求的 URL 是 http://example.com/foobar?foo=bar删除 boo=far从请求。 $guzzle_http_cl
我正在使用 Guzzle 并发请求工具: http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests 我的代码类似于
我需要将 header 类型添加到下面的 guzzle 请求中,但无法弄清楚如何在不出错的情况下添加它这是我要添加的内容: $command->set('command.headers', array
我正在使用 lumen 尝试通过 guzzle 设置简单的 api 请求。 问题是 base_uri 参数似乎没有在初始 new Client() 上正确传递. 简化示例: use GuzzleHtt
我正在使用 Guzzle 6。 我正在尝试模拟客户端并像这样使用它: 'application/json'], 'foo'), ]); $history = []; $history_middlew
我正在尝试使用 Guzzle 而不是直接使用 cURL 来实现和 HTTP 请求。我如何使用 Guzzle 发出相同类型的请求?或者我应该坚持使用cURL? $ch = curl_init(); //
Guzzle 的 API 文档在哪里? 有这两个链接(看起来是同一件事): http://guzzle.readthedocs.org/en/latest/quickstart.html http:/
我有一个页面使用 Guzzle 6 执行一些 REST 查询。它工作正常,但有时速度太慢,因为它总是进行查询。我发现有 guzzle-cache-middleware应该缓存来自远程 API 的响应。
在 Symfony 4 项目中,我使用 eightpoints/guzzle 包来调用远程 API。安装 bundle (并在单独的 yaml 文件中配置客户端)后,我关注了 this quide让我
您可以使用 Guzzle 的 Pool:batch() 方法并行执行 http 请求。它允许您使用第三个参数中的 options 键为请求设置默认选项。 但是如果我需要为池中的不同请求提供不同的选项怎
我正在尝试使用 Guzzle 5 连接到 WooCommerce API(Guzzle 6 似乎没有 oAuth 选项 o.O)。 Woocommerce requires the oAuth aut
我在 laravel 项目中遇到问题。 它显示了这个错误 Fatal Error : Class 'Guzzle\Http\Client' not found 我已安装 Guzzle 并将其添加到配置
Guzzle/Http的新手。 我有一个API rest url登录名,如果未授权,则用401代码回答;如果缺少值,则用400回答。 我将获取http状态代码以检查是否存在某些问题,但不能仅包含代码(
A. 总结 顾名思义,Guzzle 允许一次发送多个请求以节省时间,如 documentation . $responses = $client->send(array( $requestOb
我正在使用 PayPal API 模拟负面响应,以便在客户通过 onApprove 方法批准付款时获得响应并正确处理付款的关键部分。 我正在使用 GuzzleHttp + Laravel 来捕获 客户
我一直在尝试从工作中获取 GET 请求,但不断收到 500 错误。 我不知道发生了什么。 如果我执行以下操作,它可以正常工作: 'http://www.google.com', ]); $respo
我如何通过 guzzle 服务客户端获取发布数据 getCommand功能? 我的 json 如下所示: "createMessage": { "httpMethod": "P
之间有什么区别? GuzzleHttp ReactPHP Amphp 它们之间有何不同?与典型的用例一起使用是什么? 最佳答案 它们之间的主要区别是Guzzle是HTTP客户端,而Amp和ReactP
我不确定为什么会收到此错误: php composer.phar require guzzle/guzzle:~3.7 错误 : ./composer.json has been update
我想使用Guzzle和Silex将请求发送到https页面。 使用http url我有一个回应: app->get('/',function() use ($app, $client){ $r
我是一名优秀的程序员,十分优秀!