2017-01-19 5 views
0

私はguzzle php versionを使用しています:6.2.2。 Promiseが実行されている経過時間を送信することは、以下のコードで可能ですか?例えば。 5秒ごとに、経過時間を関数に送信しますか?例えば同時のGuzzle Promiseプールの経過時間を取得する方法

$client = new Client([ 
     'base_uri' => BASE_URL . 'sync/import', // Base URI is used with relative requests 
     'timeout' => 0, // 0 no timeout for operations and watching Promises 
     'verify' => true 
    ]); 

    $requests = function ($syncRequests) { 
     foreach ($syncRequests as $key => $headers) { 
      yield new Request('PUT', '', ['Content-type' => 'application/json'], json_encode(['json' => ['sync' => $headers]])); 
     } 
    }; 

    $pool = new Pool($client, $requests($this->syncRequests), [ 
     'concurrency' => 10, 
     'fulfilled' => function ($response, $index) { 
      $this->promiseFulfilled($response, $index); 
     }, 
     'rejected' => function ($reason, $index) { 
      $this->promiseRejected($reason, $index); 
     }, 
    ]); 

    $promise = $pool->promise(); // Initiate the transfers and create a promise 
    $promise->wait(); // Force the pool of requests to complete. 

$pool = new Pool($client, $requests($this->syncRequests), [ 
     'concurrency' => 10, 
     'while' => function() { // CALLED WHILE THE CONCURRENT REQUESTS ARE RUNNING!! 
      $this->elapsedTime(); 
     }, 
     'fulfilled' => function ($response, $index) { 
      $this->promiseFulfilled($response, $index); 
     }, 
     'rejected' => function ($reason, $index) { 
      $this->promiseRejected($reason, $index); 
     }, 
    ]); 

答えて

1

:だからあなたの成就関数の中で次の行を持つことができます。プール内のリクエストごとにコールバックをCURLOPT_PROGRESSFUNCTIONに接続します。これらのコールバックがトリガーされた時刻を取得し、プールを実行する前の時刻と比較することができます。

もう1つのオプションは、カスタムTaskQueuepromise library's queue() functionに挿入し、そこにカスタムロジックをフックすることです。

0

あなたが満たさ関数内の関数を呼び出すことができます。完了した関数は、要求が完了するたびに呼び出されます。

完了した関数の中で、たとえばデータベース内の要求の進行状況を更新する別の関数を呼び出すことができます。この関数は、現在のオブジェクトのメンバである可能性があります。それはあなたが"progress" request optionで何かの仕事を作ることができることが可能です

$this->UpdateProgress(); 
+0

Nadir、同時リクエストが処理中の「while」という関数を呼び出す方法はありますか?私は "達成された"ときに私は任意の機能を呼び出すことができる知っているが、私はプールが実行されている経過時間を取得する "while"要求が実行されている。 – Zelf

関連する問題