2016-07-28 52 views
1

所以即時使用Guzzle 6來進行不確定的併發api調用,但我想要做的事情之一是記錄承諾當前正在處理的數組值根據數據庫查詢結果處理api調用。之後,我想用我從api獲得的任何信息將值更新回數據庫。使用Gu 6 6併發調用並跟蹤額外參數

use GuzzleHttp\Pool; 
use GuzzleHttp\Client; 
use GuzzleHttp\Psr7\Request; 

$client = new Client(); 

$requests = function() { 
    $uri = 'http://127.0.0.1:8126/guzzle-server/perf'; 
    foreach($database_result as $res) { 

     /*the res array contains 
     ['id' => 'db id', 'query' => 'get query array']; 
     */ 

     $url = $uri . '?' . http_build_query($res['query']); 

     yield new Request('GET', $url); 
    } 
}; 

$pool = new Pool($client, $requests(), [ 
    'concurrency' => 5, 
    'fulfilled' => function ($response, $index) { 
     /** 
     * HERE i want to be able to somehow 
     * retrieve the current responses db id 
     * this way I can obviously update anything 
     * i want on the db side 
     */ 
    }, 
    'rejected' => function ($reason, $index) { 
     /** 
     * HERE i want to be able to somehow 
     * retrieve the current responses db id 
     * this way I can obviously update anything 
     * i want on the db side 
     */ 
    }, 
]); 

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

// Force the pool of requests to complete. 
$promise->wait(); 
... 

任何幫助,這將是驚人的。我想獲得關於如何最好地處理這種情況的建議。我寧願以聰明,合乎邏輯的方式去做。

謝謝你的幫助

回答

2

所以我想通了。

基本上

$requests = function() { 
    $uri = 'http://127.0.0.1:8126/guzzle-server/perf'; 
    foreach($database_result as $key => $res) { 

     /*the res array was updated to be 
     ['id' => 'get query array']; 
     */ 

     $url = $uri . '?' . http_build_query($res); 

     //here is the key difference in change 
     yield $key => new Request('GET', $url); 
    } 
}; 

現在後面的池功能指數將包含你想要的索引。

希望這會有所幫助。

參考:https://github.com/guzzle/guzzle/pull/1203