Use pthreads to implement real PHP Multithreading (PHP5.3 or later is required)

Source: Internet
Author: User
Tags php multithreading zts
PHP5.3 and later versions use the pthreadsPHP extension to enable PHP to truly support multithreading. Multithreading can greatly shorten the execution time of 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, 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:
The code is as follows:
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

Add:
The code is as follows:
Vi/Data/apps/php/etc/php. ini
Extension = "pthreads. so"

A php multi-thread and For loop is provided to capture the PHP code example on the Baidu search page:
The code is as follows:
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 <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.