Curl, используемый с pthreads, возвращает более высокое время отклика, чем обычно - PullRequest
0 голосов
/ 04 февраля 2020

Я использую curl с многопоточностью в PHP, чтобы прочитать время ответа от некоторых URL. Проблема состоит в том, что время отклика в несколько раз превышает фактическое время отклика.

Я сравнил время отклика, возвращаемого многопоточным алгоритмом, с простым PHP curl.

Я использую темы, потому что мне нужно проверить около 500 URL.

class AsyncOperation extends Thread
{
    public $is_running = true;

    public function __construct($dn)
    {
        $this->url_machine = $dn;
    }

    public function run()
    {

        $ch = curl_init('https://'.$this->machine_url.'/index.php');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        $this->response_time =  $info['total_time'];        

        $this->is_running = false;
    }
}


class telemetry_harvest 
{
    function root()
    {
        set_time_limit(0);

        $tasks = array();
        $index=0;
        //pass through the data: $domain      
        // if there are available data  do
        {
            $new_thread = new AsyncOperation($domain[$index]);            
            $new_thread->start();
            $tasks[] = $new_thread;            
        // } while ( $index++ );


        //wait until tasks finishes
        $has_finished = false;
        while(!$has_finished)
        {
            $has_finished = true;
            foreach($tasks as $task)
            {
                 if( $task->is_running )
                 {
                     $has_finished = false;
                     break;
                 }
            }

            if(!$has_finished) { sleep(10); }
        }
        //do stuff
    }    
}

$controller = new telemetry_harvest();
$controller->root();
...