Using Pthreads to implement real PHP multithreading (PHP5.3 above) _php tutorial

Source: Internet
Author: User
Tags php server php multithreading zts vps
As I said in my previous article, most Web site performance bottlenecks are not on the PHP server because it can easily be done by horizontally increasing the number of servers or CPU cores (for various cloud hosts, increasing the number of VPS or CPU cores is more convenient, directly to the backup image to increase the VPS, even the operating system, The environment is not installed), but in the MySQL database.

If you use a MySQL database, a federated query of SQL, may be able to handle the business logic, but encountered a large number of concurrent requests, the rest of the dish.

If you use a NoSQL database, you might need 10 queries to process the same business logic, but each query is faster than MySQL, 10 cycles of nosql queries may be faster than a MySQL federated query, and the tens of thousands of times/second query is completely fine.

If you add PHP multi-threading, query NoSQL at the same time through 10 threads, and return the result summary output, the speed will be faster. In our actual app, we call a PHP interface with real-time recommendation from user's liking, PHP needs to launch 500~1000 query to Bigsea NoSQL database, to calculate the user's personal preference product data in real time, PHP multi-threading is very obvious.

PHP Extensions Download: https://github.com/krakjoe/pthreads
PHP Manual Document: http://php.net/manual/zh/book.pthreads.php

1, the extension of the compilation installation (Linux), the compilation parameter--enable-maintainer-zts is a required option:
Copy CodeThe code is as follows:
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-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

Added in php.ini:
Copy the Code code as follows:
Vi/data/apps/php/etc/php.ini
Extension = "pthreads.so"

give a PHP multi-threading, and for loop, crawl Baidu search page PHP code example:
Copy CodeThe 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 + +)
{
$urls _array[] = Array ("name" = "Baidu", "url" = "http://www.baidu.com/s?wd=". Mt_rand (10000,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). "
";
?>

http://www.bkjia.com/PHPjc/765166.html www.bkjia.com true http://www.bkjia.com/PHPjc/765166.html techarticle as I said in my previous article, the performance bottleneck for most Web sites is not on the PHP server because it can be easily addressed by simply increasing the number of servers or CPU cores horizontally (for various cloud owners ...).

  • 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.