PHP 5.3 以上版本,使用pthreads PHP擴展,可以使PHP真正地支持多線程。多線程在處理重復性的循環任務,能夠大大縮短程序執行時間。
在liunx下的安裝
准備工作:
1.下載Threading for PHP安裝包https://github.com/krakjoe/pthreads
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
尼瑪還是同樣的錯誤 不知道了
windows下多線程的使用
本人php版本是5.4.17的所以下載php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包,其中0.1.0表示為當前pthreads版本號,5.4為php版本號,ts就是之前判斷php對應的ts、nts版,vs9代表是Visual Studio 2008 compiler編譯器編譯的,最后的x86代表的是32位的版本。
將下載好的php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包解壓找到
pthreadVC2.dll和php_pthreads.dll文件.
1、修改php.ini文件 添加extension=php_pthreads.dll
2、將php_pthreads.dll放到C:\wamp\bin\php\php5.5.12\ext下面
3、將pthreadVC2.dll放到C:\wamp\bin\php\php5.5.12下面
4、將pthreadVC2.dll放到C:\Windows\System32下面
5、將pthreadVC2.dll的絕對路徑放到path環境變量里面
3、重啟Apache服務器
<?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.272159099579
多線程花的時間比單線程花的時間少的多。
windows下多線程php測試成功!!!