PHPマルチスレッドの内部マルチスレッドの例分析


本論文の例はPHPマルチスレッドの内部マルチスレッド使用法を解析した。皆さんの参考にしてください。具体的には以下の通りです

class Http_MultiRequest
{
    // url
    private $urls = array();
    //curl
    private $options;
    //
    function __construct($options = array())
    {
        $this->setOptions($options);
    }
    // url
    function setUrls($urls)
    {
        $this->urls = $urls;
        return $this;
    }
    //
    function setOptions($options)
    {
        $options[CURLOPT_RETURNTRANSFER] = 1;
        if (isset($options['HTTP_POST']))
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $options['HTTP_POST']);
            unset($options['HTTP_POST']);
        }
        if (!isset($options[CURLOPT_USERAGENT]))
        {
            $options[CURLOPT_USERAGENT] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;)';
        }
        if (!isset($options[CURLOPT_FOLLOWLOCATION]))
        {
            $options[CURLOPT_FOLLOWLOCATION] = 1;
        }
        if (!isset($options[CURLOPT_HEADER]))
        {
            $options[CURLOPT_HEADER] = 0;
        }
        $this->options = $options;
    }
    //
    function exec()
    {
        if(empty($this->urls) || !is_array($this->urls))
        {
            return false;
        }
        $curl = $data = array();
        $mh = curl_multi_init();
        foreach($this->urls as $k => $v)
        {
            $curl[$k] = $this->addHandle($mh, $v);
        }
        $this->execMulitHandle($mh);
        foreach($this->urls as $k => $v)
        {
            $data[$k] = curl_multi_getcontent($curl[$k]);
            curl_multi_remove_handle($mh, $curl[$k]);
        }
        curl_multi_close($mh);
        return $data;
    }
    // 。
    function execOne($url)
    {
        if (empty($url)) {
            return false;
        }
        $ch = curl_init($url);
        $this->setOneOption($ch);
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
    // , handle
    private function setOneOption($ch)
    {
        curl_setopt_array($ch, $this->options);
    }
    // handle
    private function addHandle($mh, $url)
    {
        $ch = curl_init($url);
        $this->setOneOption($ch);
        curl_multi_add_handle($mh, $ch);
        return $ch;
    }
    // ( , ,
    // cup 100%, , 10
    // 。 PHP 。)
    private function execMulitHandle($mh)
    {
        $running = null;
        do {
            curl_multi_exec($mh, $running);
        } while ($running > 0);
    }
}
/* :*/
$urls = array("http://baidu.com", "http://baidu.com", "http://baidu.com", "http://baidu.com", "http://baidu.com", "http://baidu.com", "http://www.google.com", "http://www.sina.com.cn", );
$m = new Http_MultiRequest();
$t = microtime(true);
$m->setUrls($urls);
//parallel fetch( ):
$data = $m->exec();
$parallel_time = microtime(true) - $t;
echo $parallel_time . "
";
$t = microtime(true);
//serial fetch( ):
foreach ($urls as $url)
{
    $data[] = $m->execOne($url);
}
$serial_time = microtime(true) - $t;
echo $serial_time . "
";
本論文で述べたように、皆さんのphpプログラムの設計に役に立ちます。