search
HomeBackend DevelopmentPHP ProblemIs php multithreading useful?

Is php multithreading useful?

Oct 16, 2019 pm 01:29 PM
php

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.