0

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に似たものを作成する方法は? amp docs と stackoverflow を検索しましたが、同様のものは見つかりませんでした。

ところで、Amp は Curl をハンドラーとして使用していないこともわかりました。なぜそのようなオプションが利用できないのか理解できません。手動で追加できますか、それともさらに優れたものがありますか?curl 機能 (さまざまなカスタム ヘッダー、デバッグ/詳細の可能性など) を置き換えるものはありますか?

サポートが必要な特定のポイント:

  1. Ampフレームワークまたはそのライブラリのいずれかを使用して作成されたプールの同等の例をどこで見つけられるか、またはより単純な例でもそれを示すことができるかを誰かが教えてくれる可能性はありますか?
  2. Amp の Curl ハンドラはどこにありますか? 使用できますか?

Amphp Web サイトでは、次のように述べられています。

スタック オーバーフロー コミュニティは、十分に一般的な質問であれば回答できます。amphp タグを使用して、適切な人があなたの質問を見つけられるようにします。

十分に単純な(そして実際に機能する)例を提供したので、必要なものを正確に理解するのは簡単だと思いました。

敬意を表して。

4

1 に答える 1