Pthread multi-thread Query

Source: Internet
Author: User

 

Introduction: 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 a NoSQL database is used, it may take ten queries to complete the same business logic. At this time, PHP multithreading plays an obvious role. It can execute NoSQL queries at the same time to greatly improve the efficiency.



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.

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:

Php-5.5.1/cd/Data/tgz/
. /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:

Extension = "pthreads. so"



2. a PHP multi-thread and For loop is provided to capture the PHP code example on the baidu search page:

==== Sample code ================================================ ==============
<? 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, timeout, 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 <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";?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.