PHP Pthreads multithreaded Operation ____php

Source: Internet
Author: User

1, PHP uses the thread mode to compile--enable-maintainer-zts must add

configure_options= "'--prefix=/usr/local/php '--with-config-file-path=/usr/local/php/etc '--enable-fpm '-- With-fpm-user=www '--with-fpm-group=www '--with-mysql=mysqlnd '--with-mysqli=mysqlnd '--with-pdo-mysql= Mysqlnd '--with-iconv-dir '--with-freetype-dir=/usr/local/freetype '--with-jpeg-dir '--with-png-dir ' With-zlib '--with-libxml-dir=/usr '--enable-xml '--disable-rpath '--enable-bcmath '--enable-shmop ' Enable-sysvsem '--enable-inline-optimization '--with-curl '--enable-mbregex '--enable-mbstring '--with-mcrypt ' '--enable-ftp '--with-gd '--enable-gd-native-ttf '--with-openssl '--with-mhash '--enable-pcntl ' Enable-sockets '--with-xmlrpc '--enable-zip '--enable-soap '--with-gettext '--disable-fileinfo ' Enable-opcache ' <span style= ' color: #ff0000; >--enable-maintainer-zts ' </span> '
2, installation pthreads download path for https://github.com/krakjoe/pthreads PHP7 version of the use of v3,php5.3 above version of the V2.

After compiling the installation successfully, increase the extension=pthreads.so

Restart Nginx and PHP-FPM, prompting

Starting PHP-FPM Failed loading/usr/local/zend/zendguardloader.so:  /usr/local/zend/zendguardloader.so: Undefined symbol:executor_globals done
 
This is because PHP runs in the thread mode does not support the zendguardloader reason, can be directly ignored.

3, below feel the efficiency of multithreading

<?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 = MoD
          El_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[$th
          Read_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.c
  om/s wd= ". Mt_rand (10000,20000));
  } $t = Microtime (true);
  $result = Model_thread_result_get ($urls _array);
  $e = Microtime (true); echo "Multithreading:". ($e-$t). "

  \ n ";
  $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). "
 \ n ";
 ?>

Multithreading: 0.9154269695282 for Loop: 5.3936388492584

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.