使用GuzzleHttp:
制作了一个示例/测试代码use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Pool;
use Psr\Http\Message\ResponseInterface;
require __DIR__ . '/vendor/autoload.php';
$handler = new CurlHandler();
$stack = new HandlerStack($handler);
$stack->push(Middleware::httpErrors(), 'http_errors');
$stack->push(Middleware::redirect(), 'allow_redirects');
$stack->push(Middleware::cookies(), 'cookies');
$stack->push(Middleware::prepareBody(), 'prepare_body');
$interval = 100;
$concurrency = 50;
$client = new Client(['handler' => $stack]);
echo sprintf("Using Guzzle handler %s\n", get_class($handler));
echo sprintf("Printing memory usage every %d requests\n", $interval);
echo "Fetching package list... ";
$packageNames = json_decode(
$client->get('https://packagist.org/packages/list.json')
->getBody()
->getContents()
)->packageNames;
if (empty($packageNames)) {
echo "Empty result. No reason to continue.";
return;
}
echo 'done. (' . count($packageNames) . " packages)\n\n";
$requests = function($packageNames) {
foreach ($packageNames as $packageVendorPair) {
yield new GuzzleHttp\Psr7\Request('GET', "https://packagist.org/p/{$packageVendorPair}.json");
}
};
$pool = new Pool($client, $requests($packageNames), [
'concurrency' => $concurrency,
'fulfilled' => function (ResponseInterface $response, $index) use (&$counter, $interval) {
$counter++;
if ($counter % $interval === 0) {
echo sprintf(
"Processed %s requests. Memory used: %s MB\n",
number_format($counter),
number_format(memory_get_peak_usage()/1024/1024, 3)
);
}
},
'rejected' => function($reason, $index) use (&$counter, $interval)
{
$counter++;
if ($counter % $interval === 0) {
echo sprintf(
'Processed %s requests. Memory used: %s MB',
number_format($counter),
number_format(memory_get_peak_usage()/1024/1024, 3)
);
}
}
]);
$promise = $pool->promise();
$response = $promise->wait();
如何为Amphp或Artax制作类似的内容? 我搜索了放大器文档和stackoverflow,但无法找到类似的内容。
顺便说一句,我还发现Amp并没有使用Curl作为处理程序。不明白为什么没有这样的选择。你能手动添加它还是有更好的东西,什么取代了curl功能(各种自定义标题,调试/详细可能性等)?
我需要帮助的具体要点:
在Amphp网站上说:
Stack Overflow社区可以回答您的问题,如果它足够通用的话。使用amphp标记,以便合适的人找到您的问题。
由于我提供了足够简单(和工作)的例子,我认为很容易理解我需要的东西。
充分尊重。
答案 0 :(得分:1)
没有相应的池,但可以使用信号量和异步协程来编写。
<?php
use Amp\Artax\DefaultClient;
use Amp\Loop;
use Amp\Sync\LocalSemaphore;
require __DIR__ . "/vendor/autoload.php";
Loop::run(function () {
$concurrency = 10;
$client = new DefaultClient;
$semaphore = new LocalSemaphore(10);
$packageResponse = yield $client->request("https://packagist.org/packages/list.json");
$packageNames = json_decode(yield $packageResponse->getBody())->packageNames;
$requestHandler = Amp\coroutine(function ($package) use ($client) {
$url = "https://packagist.org/p/{$package}.json";
$response = yield $client->request($url);
$body = yield $response->getBody();
return $body;
});
$counter = 0;
foreach ($packageNames as $package) {
$lock = yield $semaphore->acquire();
$promise = $requestHandler($package);
$promise->onResolve(function ($error, $body) use (&$counter, $lock) {
$lock->release();
if (++$counter % 50 === 0) {
echo sprintf(
"Processed %s requests. Memory used: %s MB\n",
number_format($counter),
number_format(memory_get_peak_usage()/1024/1024, 3)
);
}
});
}
});
此示例使用LocalSemaphore
实现,该实现是Amp\Sync\Semaphore
的实现。信号量用于限制并发性。
Amp中没有卷曲处理程序,因为它不能很好地处理事件循环。 Curl有自己的事件循环,但只允许多个并发HTTP请求,而不允许其他非阻塞I / O.这就是为什么Artax基于原始PHP套接字实现HTTP而不依赖于Curl的原因。