Open
Description
given the following snippet
#!/usr/bin/env php
<?php
require_once "vendor/autoload.php";
use Http\Client\Curl\Client;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
$messageFactory = MessageFactoryDiscovery::find();
$client = new Client($messageFactory, StreamFactoryDiscovery::find());
$client->sendAsyncRequest(
$messageFactory->createRequest('GET', 'https://httpstat.us/200?sleep=3000')
)->wait(true)->getBody()->getContents();
the service"
https://httpstat.us/200?sleep=3000
simulate a slow request waiting for 3 seconds.
Where running the script, my application consume A LOT of CPU while waiting for the request
$ time ./index.php
real 0m3.190s
user 0m1.638s
sys 0m1.523s
Here CPU is used 1.638s + 1.523s = 3.161s to process this 3.190s script
when applying this durty patch
diff --git a/src/MultiRunner.php b/src/MultiRunner.php
index 545f39a..5872804 100644
--- a/src/MultiRunner.php
+++ b/src/MultiRunner.php
@@ -82,6 +82,7 @@ class MultiRunner
public function wait(PromiseCore $targetCore = null)
{
do {
+ usleep(1);
$status = curl_multi_exec($this->multiHandle, $active);
$info = curl_multi_info_read($this->multiHandle);
if (false !== $info) {
I get
$ time ./index.php
real 0m3.163s
user 0m0.234s
sys 0m0.211s
now, the CPU is used 0.445s to process the same request.