Use pthreads to implement real PHP multithreading

Source: Internet
Author: User
Tags php multithreading vps
Using pthreads to implement real PHP multithreading I mentioned in my previous article that 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, increasing the number of VPS or CPU cores is more convenient. you can directly increase the number of VPS by using backup images, you do not need to install or configure the operating system or environment), but 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. extended compilation and installation (Linux). the compilation 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 install        unzip pthreads-master.zipcd pthreads-master/Data/apps/php/bin/phpize./configure --with-php-config=/Data/apps/php/bin/php-configmakemake install

Add:

vi /Data/apps/php/etc/php.iniextension = "pthreads.so"

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 ). ""; $ 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 ). "";?>

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.