Most Web site performance bottleneck is not on the PHP server, because it can simply increase the number of servers or CPU to easily handle (for a variety of cloud hosts, increase the number of VPS or CPU cores more convenient, directly to the backup image to increase the VPS, even the operating system, the environment is not installed configuration), It is the MySQL database. If you use a MySQL database, a federated query of SQL, may be able to handle the business logic, but encountered a large number of concurrent requests, the rest of the dish. If you use a NoSQL database, you might need 10 queries to process the same business logic, but each query is faster than MySQL, 10 cycles of nosql queries may be faster than a MySQL federated query, and the tens of thousands of times/second query is completely fine. If you add PHP multi-threading, query NoSQL at the same time through 10 threads, and return the result summary output, the speed will be faster. In our actual app, we call a PHP interface with real-time recommendation from user's liking, PHP needs to launch 500~1000 query to Bigsea NoSQL database, to calculate the user's personal preference product data in real time, PHP multi-threading is very obvious.
PHP Extensions Download: https://github.com/krakjoe/pthreads
PHP Manual Document: http://php.net/manual/zh/book.pthreads.php
PHP Multi-Threading implementation
Extended compilation installation (Linux), edit parameter--enable-maintainer-zts is a must option:
cd/data/tgz/php-5.5.1
./configure--prefix=/data/apps/php--with-config-file-path=/data/apps/php/etc--with-mysql=/Data/apps/mysql-- With-mysqli=/data/apps/mysql/bin/mysql_config--with-iconv-dir--with-freetype-dir=/data/apps/libs-- With-jpeg-dir=/data/apps/libs--with-png-dir=/data/apps/libs--with-zlib--with-libxml-dir=/usr--enable-xml-- Disable-rpath--enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization--with-curl-- Enable-mbregex--enable-fpm--enable-mbstring--with-mcrypt=/data/apps/libs--with-gd--enable-gd-native-ttf-- With-openssl--with-mhash--enable-pcntl--enable-sockets--with-xmlrpc--enable-zip--enable-soap--enable-opcache-- With-pdo-mysql--enable-maintainer-zts
Make clean
Make
Make install
Unzip Pthreads-master.zip
CD Pthreads-master
/data/apps/php/bin/phpize
./configure--with-php-config=/data/apps/php/bin/php-config
Make
Make install
Vi/data/apps/php/etc/php.ini
Add to:
Extension = "pthreads.so"
Give a PHP multi-threading, and for loop, crawl Baidu search page PHP code example
-
- 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.baidu.com/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 ";
- ?>
Original: http://blog.s135.com/pthreads/Zhang Banquet
http://www.bkjia.com/PHPjc/739145.html www.bkjia.com true http://www.bkjia.com/PHPjc/739145.html techarticle Most web site performance bottlenecks are not on the PHP server because it can easily be addressed by simply increasing the number of servers or CPU cores horizontally (for various cloud hosts, increasing the number of VPS or CPU cores ...)