PHP 5.3 以上版本,使用pthreads PHP擴展,可使PHP真正地支持多線程。多線程在處理重複性的循環任務,可以大大縮短程序執行時間。php
在liunx下的安裝git
準備工做:github
1.下載Threading for PHP安裝包https://github.com/krakjoe/pthreadswindows
2.php安裝包安全
php安裝時必定要加上--enable-maintainer-zts參數 這個是安全線程服務器
yum install php-devel php-pear httpd-devel
wget http://www.php.net/distributions/php-5.5.8.tar.gz
tar zxvf php-5.5.8.tar.gz
cd php-5.5.8.tar.gz
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-maintainer-zts
make && make install
( make -j3 && make -j3 install) -> Faster building
cp php.ini-development /etc/php.ini
pecl install pthreads
提示錯誤
error: pthreads requires ZTS, please re-compile PHP with ZTS enabled
不對啊 我加了安全線程參數了 怎麼還報錯。。。
好像剛纔用yum安裝php-devel的時候安裝了php
因而
yum remove php
從新編譯在安裝
pecl install pthreads
提示錯誤 ERROR: `phpize' failed
cd /usr/local/php/bin/
查看是否有phpize
發現有phpize,說明不是php-devel的問題
是安裝擴展的方式出了問題,因此php安裝是沒有問題的
下載pthreads擴展包
unzip pthreads-master.zip
cd pthreads
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
提示Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20121212/
說明安裝成功
修改php.ini文件,添加extension=pthreads.so
echo "extension=pthreads.so" >> /etc/php.ini
php -v
提示Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pthreads.so' - /usr/lib64/php/modules/pthreads.so: cannot open shared object file: No such file or directory in Unknown on line 0
尼瑪都沒一件順心的安裝
錯誤 我擦
查看一下 /usr/local/php/include/php/main/php_config.php 裏面是否有#define ZTS 1
裏面有這個啊 我擦 這是什麼問題呢 繼續百度
從新安裝一次試試看
cd php-5.5.8.tar.gz
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-maintainer-zts
make -j3 && make -j3 install
cp php.ini-development /etc/php.ini
進入pthreads解壓目錄
cd ~/soft/pthreads-master
運行phpize腳本
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
提示安裝成功
echo "extension=pthreads.so" >> /etc/php.ini
php -v
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pthreads.so' - /usr/lib64/php/modules/pthreads.so: undefined symbol: core_globals_id in Unknown on line 0多線程
尼瑪仍是一樣的錯誤 不知道了curl
windows下多線程的使用測試
<?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); } } } 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 : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)'; $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 < 10; $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"; ?>
多線程:5.1022920608521 For循環:20.272159099579ui