Home  >  Article  >  Backend Development  >  Is php multithreading useful?

Is php multithreading useful?

(*-*)浩
(*-*)浩Original
2019-10-16 13:29:332869browse

PHP 5.3 and above, using the pthreads PHP extension, can make PHP truly support multi-threading. Multiple threads handle repetitive cyclic tasks, which can greatly shorten program execution time.

Is php multithreading useful?

I said in my previous article that the performance bottleneck of most websites is not on the PHP server, because it can be simply increased by adding servers laterally or It can be easily handled by the number of CPU cores (for various cloud hosts, it is more convenient to increase the number of VPS or CPU cores. You can add VPS directly with a backup image, without even installing and configuring the operating system and environment), but it lies in the MySQL database. (Recommended learning: PHP video tutorial)

If you use a MySQL database and a joint query SQL, you may be able to handle the business logic. However, if you encounter a large number of concurrent requests, you will have to stop. .

If you use a NoSQL database, it may take ten queries to process the same business logic, but each query is faster than MySQL. Ten loop NoSQL queries may be faster than a MySQL joint query. Tens of thousands of queries/second are no problem at all. If you add PHP multi-threading, query NoSQL through ten threads at the same time, and return the result summary output, the speed will be faster.

In our actual APP product, we call a PHP interface that recommends products in real time based on user preferences. PHP needs to initiate 500 to 1,000 queries to the BigSea NoSQL database to calculate the user's personalized preference product data in real time. PHP has many The role of threads is very obvious.

Gives a PHP multi-threading and For loop to capture the PHP code example of Baidu search page:

<?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_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 : &#39;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)&#39;; 
       $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 < 100; $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 "多线程:".($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循环:".($e-$t)."\n";
 ?>

The above is the detailed content of Is php multithreading useful?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn