按照文檔中并發(fā)請(qǐng)求 使用的,結(jié)果確實(shí)時(shí)間疊加,并沒有速度快
這里寫問題描述
$client = new Client();
$requests = function ($total) {
$uri = 'http://www.baidu.com';
for ($i = 0; $i < $total; $i++) {
yield new GRequest('GET', $uri);
}
};
$pool = new Pool($client, $requests(10), [
'concurrency' => 10,
'fulfilled' => function ($response, $index) {
// this is delivered each successful response
},
'rejected' => function ($reason, $index) {
// this is delivered each failed request
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
你那個(gè)GRequest里面是不是用requestAsync請(qǐng)求?
這個(gè)文檔中的源碼 ,在webman中 有Request 沖突 所以用了 as 別名,就是文檔中的源碼,但是請(qǐng)求10次的時(shí)間 是 請(qǐng)求一次的時(shí)間 的10倍,發(fā)現(xiàn)并發(fā)沒起作用
你那個(gè)GRequest里面是不是用requestAsync請(qǐng)求?
$requests = function () use ($client, $total) {
$uri = 'http://www.baidu.com';
for ($i = 0; $i < $total; $i++) {
yield function() use ($client, $uri) {
return $client->requestAsync('GET', $uri);
};
}
};
不是呀,這個(gè)是文檔中的 yield new Request('GET', $uri); Request 不是我封裝的,這個(gè)和webman 的support\Request有名字沖突,就起了個(gè)別名
用你代碼試了,沒這個(gè)疊加的問題
本機(jī)測(cè)試,total = 1 , 耗時(shí)70ms, total 1-10 耗時(shí) 350ms . total 10以上 約等于 total / 10 * 350
total10的時(shí)候,concurrency用的1-10都差不多時(shí)間,你調(diào)整total和concurrency,你會(huì)發(fā)現(xiàn)到達(dá)一個(gè)閾值的時(shí)候,就是疊加,猜測(cè)是本身并concurrency有上限
你可以測(cè)試一下請(qǐng)求一次和請(qǐng)求五次,concurrency 設(shè)置成5試試,如果時(shí)間基本一致,那就是對(duì)了,如果還是時(shí)間疊加,那就不對(duì)