PHP5.3 and later versions use the pthreadsPHP extension to enable PHP to truly support multithreading. Multithreading can greatly shorten program execution time when processing repetitive cyclic tasks. As I mentioned in my previous article, the performance bottleneck of most websites is not on the PHP server, because it can be easily handled by adding servers or CPU cores horizontally (for various cloud hosts, it is more convenient to increase the number of VPS or CPU cores.
PHP 5.3 and later versions use the pthreads PHP extension to enable PHP to truly support multithreading. Multithreading can greatly shorten program execution time when processing repetitive cyclic tasks.
As I mentioned in my previous article, the performance bottleneck of most websites is not on the PHP server, because it can be easily handled by adding servers or CPU cores horizontally (for various cloud hosts, it is more convenient to increase the number of VPS or CPU cores. you can directly add VPS to the backup image without installing the configuration in the operating system or environment), but in the MySQL database. If you use a MySQL database and a SQL statement for joint query, you may be able to process the business logic. However, if you encounter a large number of concurrent requests, you can stop. If a NoSQL database is used, it may take ten queries to complete the same business logic. However, each query is faster than MySQL, and the ten-round NoSQL queries may be faster than one MySQL joint query, there is no problem in dealing with tens of thousands of queries per second. If PHP multithreading is added and NoSQL is queried through ten threads at the same time, and the returned results are summarized and output, the speed will be faster. In our actual APP product, we call a PHP interface that recommends commodities in real time through user preferences. PHP needs to initiate 500 ~ 1000 queries to calculate the user's personal preferences and commodity data in real time. the Function of PHP multithreading is very obvious.
PHP extensions: https://github.com/krakjoe/pthreads
PHP Manual: http://php.net/manual/zh/book.pthreads.php
1. for the extended compilation and installation (Linux), the edit parameter -- enable-maintainer-zts is required:
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-ztsmake cleanmakemake installunzip pthreads-master.zipcd pthreads-master/Data/apps/php/bin/phpize./configure --with-php-config=/Data/apps/php/bin/php-configmakemake install
Note: If an error is reported during installation, for example, the following error occurs during installation in ubuntu:
. /Classes/thread. h: In the 'zim _ Thread_kill 'function :. /classes/thread. h: 288: 6: Error: 'sigusr1' is not declared (used for the first time in this function ). /classes/thread. h: 288: 6: Note: Each undeclared identifier only reports make: *** [php_pthreads.lo] Error 1 in the function that appears.
Go to http://pecl.php.net/package/pthreadsto download pthread-0.0.45.tgzinstallation.
vi /Data/apps/php/etc/php.ini
Add:
extension = "pthreads.so"
2. a PHP multi-thread and For loop is provided to capture the PHP code example on the Baidu search page:
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 <100; $ I ++ )?? {$ Urls_array [] = array ("name" => "baidu", "url" =>" http://www.baidu.com/s?wd= ". Mt_rand (Random, 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";?>
Output result:
Multithreading: 8.8592751026154
For loop: 34.447397947311
The results show that multithreading is much faster than normal loops.