PHP Real multi-threaded use

Source: Internet
Author: User
Tags php server vars zts vps

PHP 5.3 or later, using the pthreads PHP extension, you can make PHP really support multi-threading. Multithreading in the processing of repetitive cyclic tasks, can greatly shorten the program execution time.

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, extended compilation installation (Linux), edit parameter--enable-maintainer-zts is a must option:

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



Vi/data/apps/php/etc/php.ini


Add to:

Extension = "pthreads.so"



2, give a section of PHP multi-threading, and for loop, crawl Baidu search page PHP code example:

View Plainprint?
    1. <?php
    2. class Test_thread_run extends thread
    3. {
    4. public $url;
    5. public $data;
    6. Public function __construct ($url)
    7. {
    8. $this->url = $url;
    9. }
    10. Public function run ()
    11. {
    12. if (($url = $this->url))
    13. {
    14. $this->data = model_http_curl_get ($url);
    15. }
    16. }
    17. }
    18. function Model_thread_result_get ($urls _array)
    19. {
    20. foreach ($urls _array as $key = $value)
    21. {
    22. $thread _array[$key] = new Test_thread_run ($value ["url"]);
    23. $thread _array[$key]->start ();
    24. }
    25. foreach ($thread _array as $thread _array_key = $thread _array_value)
    26. {
    27. While ($thread _array[$thread _array_key]->isrunning ())
    28. {
    29. Usleep (10);
    30. }
    31. if ($thread _array[$thread _array_key]->join ())
    32. {
    33. $variable _data[$thread _array_key] = $thread _array[$thread _array_key]->data;
    34. }
    35. }
    36. return $variable _data;
    37. }
    38. function Model_http_curl_get ($url,$userAgent ="")
    39. {
    40. $userAgent = $userAgent? $userAgent: ' mozilla/4.0 (compatible; MSIE 7.0;   Windows NT 5.2) ';
    41. $curl = Curl_init ();
    42. curl_setopt ($curl, Curlopt_url, $url);
    43. curl_setopt ($curl, Curlopt_returntransfer, 1);
    44. curl_setopt ($curl, Curlopt_timeout, 5);
    45. curl_setopt ($curl, curlopt_useragent, $userAgent);
    46. $result = curl_exec ($curl);
    47. Curl_close ($curl);
    48. return $result;
    49. }
    50. For ($i = 0; $i < 100; $i + +)
    51. {
    52. $urls _array[] = Array ("name" = "Baidu", "url" = " http://www.baidu.com/s?wd=". Mt_rand (  10000,20000));
    53. }
    54. $t = Microtime (true);
    55. $result = model_thread_result_get ($urls _array);
    56. $e = Microtime (true);
    57. echo "Multithreading:". ( $e-$t).  "\ n";
    58. $t = Microtime (true);
    59. foreach ($urls _array as $key = $value)
    60. {
    61. $result _new[$key] = model_http_curl_get ($value ["url"]);
    62. }
    63. $e = Microtime (true);
    64. echo "for Loop:". ( $e-$t).  "\ n";
    65. ?>

PHP Real multi-threaded use

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.