PHP擴充之多執行緒

墨辰丷
發布: 2023-03-25 21:38:02
原創
2436 人瀏覽過

本篇主要介紹PHP多線程,有興趣的朋友參考下,希望對大家有幫助。

PHP 5.3 以上版本,使用pthreads PHP擴展,可以讓PHP真正支援多執行緒。
多執行緒在處理重複性的循環任務,能夠大幅縮短程式執行時間。
PHP擴充下載:https://github.com/krakjoe/pthreads
PHP手冊文件:http://php.net/manual/zh/book.pthreads.php
1、擴充的編譯安裝(Linux),編輯參數–enable-maintainer-zts 是必選項:

'./configure'
'--prefix=/usr/local/webserver/php'
'--with-config-file-path=/usr/local/webserver/php/etc'
'--with-apxs2=/usr/local/webserver/httpd/bin/apxs'
'--with-libxml-dir=/usr/local/webserver/libxml2'
'--with-mysql=/usr/local/webserver/mysql'
'--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config'
'--enable-soap' '--enable-sockets' '--with-curl' --enable-maintainer-ztsmake clean
make
make install
unzip pthreads-master.zip
cd pthreads-master
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
登入後複製
makemake install
登入後複製
vi /usr/local/webserver/php/etc/php.ini
登入後複製

新增

extension = "pthreads.so"
登入後複製

2、給一段PHP多執行緒、與For循環,抓取百度搜尋頁面的PHP程式碼範例:

<?php
class test_thread_run extends Thread{
    public $url;    public $data;    public function __construct($url)
    {
        $this->url = $url;
    }    public function run()
    {
        if(($url = $this->url))
        {        $this->data = model_http_curl_get($url);
        }
    }
}
登入後複製
<?php
function model_thread_result_get($urls_array){
    foreach ($urls_array as $key => $value)
    {        $thread_array[$key] = new test_thread_run($value["url"]);        
                 $thread_array[$key]->start();
    }    
    foreach ($thread_array as $thread_array_key => $thread_array_value)
    {        
    while($thread_array[$thread_array_key]->isRunning())
        {
        usleep(10);
        }        
        if($thread_array[$thread_array_key]->join())
        {            
        $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;
        }
    }    
    return $variable_data;
}
function model_http_curl_get($url,$userAgent="")
{
    $userAgent = $userAgent ? $userAgent : &#39;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)&#39;;    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);    
    $result = curl_exec($curl);
    curl_close($curl);    return $result;
}
for ($i=0; $i < 100; $i++)
{    
        $urls_array[] = array("name" => "baidu", "url" =>    
        "http://www.baidu.com/s?wd=".mt_rand(10000,20000));
    }    $t = microtime(true);    
    $result = model_thread_result_get($urls_array);    
    $e = microtime(true);    
    echo "多线程:".($e-$t)."\n";    
    $t = microtime(true);    
    foreach ($urls_array as $key => $value)
    {    
    $result_new[$key] = model_http_curl_get($value["url"]);
}
$e = microtime(true);
echo "For循环:".($e-$t)."\n";
?>
登入後複製

相關推薦:

##H5 WebWorkers多執行緒開發使用詳解

PHP多執行緒模擬實作秒殺搶單一

PHP實作多進程與多執行緒

以上是PHP擴充之多執行緒的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!