Php “究竟是什么?”;“并发性”;狂饮?
我在Php “究竟是什么?”;“并发性”;狂饮?,php,concurrency,guzzle,Php,Concurrency,Guzzle,我在池中没有找到关于并发选项的太多信息 如果这是可以在服务器上打开的TCP套接字数量,那么问题是“为了更快地处理请求,我可以使用多少并发?” 我有一个使用池的示例: // I am using Laravel, this is basically retrieving entities from DB $exchangers = Exchanger::all(); $client = new Guzzlelient(); $requests = [];
池
中没有找到关于并发
选项的太多信息
如果这是可以在服务器上打开的TCP套接字数量,那么问题是“为了更快地处理请求,我可以使用多少并发?”
我有一个使用池的示例:
// I am using Laravel, this is basically retrieving entities from DB
$exchangers = Exchanger::all();
$client = new Guzzlelient();
$requests = [];
foreach ($exchangers as $exchanger)
{
$requests[$exchanger->id] = new Request('GET', $exchanger->xml_feed_url);
}
$pool = new Pool($client, $requests, [
'concurrency' => 5,
'options' => [
'verify' => false
],
'fulfilled' => function ($response, $index) {
echo "fulfilled: " . $index."\n";
},
'rejected' => function ($reason, $index) {
echo "rejected: " . $index. "\n";
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
在并发设置为5的情况下,大约花了10秒的时间浏览了20个站点。
现在我想,“好的,这是套接字的数量。套接字~端口。我有65535个端口。酷,为什么不设置并发50,我应该在一秒钟左右得到所有结果?”。很好,我把它设为50,然后。。。花了8秒。然而,一小时前的结果是18秒对24秒(对于50个并发,所以速度更慢)
因此,问题是:
如何确定我可以使用哪种并发性来优化进程并使其尽可能快
什么是并发
这就是你所期望的。您发送X个并发请求,但同时只发送并发
请求。每当一个请求完成时,另一个请求就会排队()
以下是如何在源代码中完成此操作:
private function refillPending()
{
if (!$this->concurrency) {
// Add all pending promises.
while ($this->addPending() && $this->advanceIterator());
return;
}
// Add only up to N pending promises.
$concurrency = is_callable($this->concurrency)
? call_user_func($this->concurrency, count($this->pending))
: $this->concurrency;
$concurrency = max($concurrency - count($this->pending), 0);
// Concurrency may be set to 0 to disallow new promises.
if (!$concurrency) {
return;
}
// Add the first pending promise.
$this->addPending();
// Note this is special handling for concurrency=1 so that we do
// not advance the iterator after adding the first promise. This
// helps work around issues with generators that might not have the
// next value to yield until promise callbacks are called.
while (--$concurrency
&& $this->advanceIterator()
&& $this->addPending());
}
每次请求完成时都会调用此方法(函数步骤设置为在每次成功调用failiure回调后调用)
但是,一般来说,更多并不意味着更好,因为您可能会遇到其他限制,如操作系统套接字、ISP速率限制或远程服务器速率限制(如果所有请求都指向同一服务器)。在大多数情况下,通过反复试验找到最佳值 嗯,我所有的请求都会转到不同的服务器。所以我认为操作系统可以打开50个插座,那么为什么我看不到时间上的巨大差异呢。比如25个URL,50个套接字,每个请求1-2秒?:)本地带宽可能也是一个限制因素。如果您将足够多的响应排队,则可能会丢弃数据包,并且必须重新请求它们,从而导致吞吐量降低。也许可以检查你的网络流量,看看你的利用率。