Using Pthreads to implement a true PHP multi-threading approach

Source: Internet
Author: User
Tags php class php server vps
This article mainly introduces the use of pthreads implementation of the real PHP multi-threading method, has a certain reference value, now share to everyone, the need for friends can refer to

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, the extension of the compilation installation (Linux), the compilation parameter--enable-maintainer-zts is a required 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-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

Added in php.ini:

Vi/data/apps/php/etc/php.iniextension = "Pthreads.so"

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

<?php 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_htt          P_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] = $th          read_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). "";? >

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.