PHP Extended Download: https://github.com/krakjoe/pthreads
PHP Manual Documentation: http://php.net/manual/zh/book.pthreads.php
Install script
| The code is as follows |
Copy Code |
#!/bin/sh
cd/web/soft/php
If [-D "Pthreads-master"];then
rm-rf pthreads-master
Fi
Unzip Pthreads-master.zip
CD Pthreads-master
/web/server/php/bin/phpize
./configure--with-php-config=/web/server/php/bin/php-config
Make
Make install
rm-rf pthreads-master
phpini= "/web/server/php/etc/php.ini"
sed-i ' 907a extension = "pthreads.so" $PHPINI
#更新php-fpm configuration
sed-i ' s%;p id = run/php-fpm.pid%pid = run/php-fpm.pid% '/web/server/php/et c/php-fpm.conf
sed-i ' s%;error_log = Log/php-fpm.log%error_log = log/php-fpm.log% '/web/server/php/etc/ php-fpm.conf
#杀死php-fpm process
PS aux | grep "php" | grep-v "grep" | awk ' {print $} ' | xargs-i kill-9 {}
#启动php -FPM
/WEB/SERVER/PHP/SBIN/PHP-FPM
An error occurred during Setup
Configure:WARNING:You'll need re2c 0.13.4 or later if you wan T to regenerate PHP parsers
|
The workaround is to install or upgrade RE2C version 0.13.4.
Below we use the RPM package to install this library.
Centos-5 32-bit: http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el5.rf.i386.rpm
Centos-5 64-bit: http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el5.rf.x86_64.rpm
Centos-6 32-bit: http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el6.rf.i686.rpm
Centos-6 64-bit: http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el6.rf.x86_64.rpm
Configure:error:pthreads requires Zts, please re-compile PHP with ZTS enabled
Reason: I did not join the--enable-maintainer-zts when compiling PHP, this must recompile PHP, cannot dynamically load!
So I recompile PHP, in the original compilation parameters based on that added to the--ENABLE-MAINTAINER-ZTS, recompile install PHP can!
The following is an example
| The code is as follows |
Copy Code |
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 < $i + +)
{
$urls _array[] = Array ("name" => "Baidu", "url" => "http://www.111cn.net/s?wd=". Mt_rand (10000,20000));
}
$t = Microtime (true);
$result = Model_thread_result_get ($urls _array);
$e = Microtime (true);
echo "Multithreading:". ($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 loop:". ($e-$t). " \ n ";
?> |