PHP real multi-thread usage

Source: Internet
Author: User
Tags php multithreading zts
: This article mainly introduces how to use PHP with true multithreading. For more information about PHP tutorials, see. 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:

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-c/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:

 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";?>

The above introduces the use of PHP's true multithreading, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.