2017-02-20 15 views
2

がつがつ食うHTTP非同期プール要求

$client = new Client($conf); 

//Pool of requests 
$requests = function ($data) use ($client) { 
    yield function() use ($client, $pixel) { 
     .... 
    } 
} 
//Pool of fullfilled 
$fullfilled = function ($response, $index) use ($data) { 
    ... 
} 
//Pool of rejected 
$rejected = function ($reason, $index) use ($data) { 
    ... 
} 

$pool = new Pool($client, $requests($data), [ 
      'concurrency' => 10, 'fulfilled' => $fullfilled, 'rejected' => $rejected 
     ]); 

// Initiate the transfers and create a promise 
$promise = $pool->promise(); 
$promise->wait(); 

を私は適切に「待機」、しかし、私は、プロセスが非同期になりたいを使用していusreありませんよ。 現在、リクエストがハングアップしていますが、待機を省略すると、プールはまったく送信されません。 アイデア?

+0

は私には正常に見えます。 'wait'は同期メソッドなので、すべてのリクエストが解決されるまでスクリプトは"ハングアップ "しています。 –

+0

@AlexBlexしかし、私が待機を省略すると、要求はまったく送信されません。 –

+0

はい、 'wait'は' curl_multi_exec'を起動してすべてのリクエストを並行して実行します。本当に非同期で、httpを送信したり、何かをしたり、レスポンスを処理したりする必要がある場合は、自分でキューを「チェック」する必要があります。例はhttps://github.com/guzzle/guzzle/issues/1127をご覧ください。 –

答えて

1

がつがつ食うのドキュメントには、約束を使用して複数の同時要求を作成する方法について説明します:http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests

+0

はい、私はまだ待つ必要があるので、これは私の質問に答えません。 $ client = new Client(); $ requests = function($ total)use($ client){ $ uri = 'http://127.0.0.1:8126/guzzle-server/perf'; for($ i = 0; $ i <$ total; $ i ++){ ($ client、$ uri){ return $ client-> getAsync($ uri); }; } }; $ pool =新しいプール($ client、$ requests(100)); –

関連する問題