PHP multi-Threading using _php Tutorial

Source: Internet
Author: User
Tags php server zts vps
Most Web site performance bottleneck is not on the PHP server, because it can simply increase the number of servers or CPU to easily handle (for a variety of cloud hosts, increase the number of VPS or CPU cores more convenient, directly to the backup image to increase the VPS, even the operating system, the environment is not installed configuration), It is 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


PHP Multi-Threading implementation


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"

Give a PHP multi-threading, and for loop, crawl Baidu search page PHP code example

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

Original: http://blog.s135.com/pthreads/Zhang Banquet

http://www.bkjia.com/PHPjc/739145.html www.bkjia.com true http://www.bkjia.com/PHPjc/739145.html techarticle Most web site performance bottlenecks are not on the PHP server because it can easily be addressed by simply increasing the number of servers or CPU cores horizontally (for various cloud hosts, increasing the number of VPS or CPU cores ...)

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