Home Backend Development PHP Tutorial PHP's support and use of multi-threaded programming

PHP's support and use of multi-threaded programming

Dec 22, 2017 pm 05:38 PM
php Multithreading programming

In the field of PHP, the concept of multi-threading is not as well-known as other languages. I thought that PHP is generally a single-threaded model and is not suitable for multi-threaded fields. After looking through the source code of some multi-threaded projects, I found that PHP's multi-threading also has great uses. When used flexibly, it turns out to be very suitable for solving certain problems.

Multi-threading

Threads

First let’s talk about threads:

Thread (thread) is the smallest unit that the operating system can perform calculation scheduling. It is included in the process and is the actual operating unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can run concurrently in a process, and each thread performs different tasks in parallel.

The main reason for using multi-threading is because it improves execution efficiency. A big advantage. Since a thread is the smallest unit that the operating system can schedule:

A multi-threaded program has a greater probability of being scheduled by the operating system than a single-threaded program, so multi-threaded programs are generally more efficient than single-threaded programs;

Multiple threads of a multi-threaded program can run simultaneously on multiple cores of a multi-core CPU, which can fully take advantage of the multi-core machine;

Compared with multi-process programs at the same time, multi-threading has the following characteristics:

The system overhead of creating and switching threads is smaller than that of processes, so it is more efficient than multiple processes to a certain extent;

Threads are born with shared memory space, and communication between threads is simpler. Avoids the introduction of new complexity by process IPC.

Applicable scenarios

There are many optimizations for multi-threading, but mindless use of multi-threading cannot improve the execution efficiency of the program, because the creation and destruction of threads, context switching, thread synchronization, etc. also have Performance loss may take more time than sequential execution of code. For example:

sumSmall is a function that accumulates from 1 to 50000.

PHPs support and use of multi-threaded programming

The above picture is a comparison of the time when sumSmall is executed three times in the main thread and sumSmall is executed in three threads respectively, and then the results are synchronized to one thread. We will find that only The execution time of the main thread is actually shorter. The time for creating, switching, and synchronizing the three threads is far greater than the time saved by asynchronous execution of the threads.

The function sumLarge accumulates from 1 to 5000000. The following figure shows the time it takes to execute the same thread three times and three threads:

PHPs support and use of multi-threaded programming

This time, multi-threading Finally there is an efficiency advantage.

Whether to use multi-threading depends on specific needs. Generally, the following two situations are considered:

I/O blocking will cause task scheduling in the operating system and block the current task, so in the code When there is a lot of I/O, the code can be parallelized when using multi-threading. For example, reading an entire file multiple times, or requesting multiple network resources.

Multi-threading can make full use of the CPU, so when there are multiple computationally intensive codes, you can also use multi-threading to execute them in parallel, such as the latter example above.

Multi-threading in PHP

PHP does not support multi-threading by default. To use multi-threading, you need to install the pthread extension. To install the pthread extension, you must use the --enable-maintainer-zts parameter. Recompile PHP. This parameter specifies the use of thread safety when compiling PHP.

Thread safety

Multi-threading is a factor that makes the program restless. Before using multi-threading, you must first consider thread safety issues:

Thread safety: Thread safety Safety is a term in programming, which means that when a function or function library is called in a multi-threaded environment, it can correctly handle shared variables between multiple threads, so that the program function can be completed correctly.

In traditional multi-threading, since multiple threads share variables, the following problems may occur:

There is a global array $arr = array('a');;

A thread gets the array length to be 1;

B thread gets the array length to be 1;

A thread pops out the array element $a = array_pop($arr); $a = 'a';;

B thread also pops the array element $b = array_pop($arr); $a = null;;

At this time, a supernatural event occurred in B thread, obviously The length of the array is greater than 0, or nothing pops out;

PHP implementation

The thread safety implemented by PHP mainly uses the TSRM mechanism to isolate global variables and static variables, and separate global variables and static variables. The variables are copied to each thread, and each thread uses a backup of the main thread, thus avoiding variable conflicts and thread safety issues.

PHP's multi-thread encapsulation ensures thread safety. Programmers no longer need to consider adding various locks to global variables to avoid read and write conflicts. It also reduces the chance of errors and makes the code written more secure.

But the result is that once the sub-thread starts running, the main thread can no longer adjust the running details of the sub-thread, and the thread loses the ability to transmit messages between threads through global variables to a certain extent. .

At the same time, after PHP turns on the thread safety option, there will be additional losses when using the TSRM mechanism to allocate and use variables. Therefore, in a PHP environment that does not require multi-threading, use the ZTS (non-thread safety) version of PHP. Just fine.

Classes and methods

PHP encapsulates threads into the Thread class. The creation of a thread is achieved by instantiating a thread object. Due to the encapsulation of the class, the use of variables can only be passed in through the constructor, and the thread operation results also need to be passed through class variables. outgoing.

The following introduces several commonly used Thread class methods:

run():此方法是一个抽象方法,每个线程都要实现此方法,线程开始运行后,此方法中的代码会自动执行;
start():在主线程内调用此方法以开始运行一个线程;
join():各个线程相对于主线程都是异步执行,调用此方法会等待线程执行结束;
kill():强制线程结束;
isRunning():返回线程的运行状态,线程正在执行run()方法的代码时会返回 true;
Copy after login

Due to the implementation of thread safety, after PHP multi-threads start running, they can no longer communicate through the shared memory space, and threads cannot communicate through threads. Communication reuse, so I think PHP's "thread pool" is meaningless. The Pool class that comes with the extension is a class that manages multi-thread allocation and will not be introduced here.

Example code

The following is a thread class used to request a certain interface. Next, write two multi-threaded application examples based on it:

class Request extends Thread {
    public $url;
    public $response;
    public function __construct($url) {
        $this->url = $url;
    }
    public function run() {
        $this->response = file_get_contents($this->url);
    }
}
Copy after login

Asynchronous request

Split the synchronous request into multiple threads for asynchronous calls to improve the running efficiency of the program.

$chG = new Request("www.google.com");
$chB = new Request("www.baidu.com");
$chG ->start();
$chB ->start();
$chG->join();
$chB->join();
$gl = $chG->response;
$bd = $chB->response;
Copy after login

Timeout control

I accidentally discovered that a piece of content on a certain webpage of the company's website comes and goes. I don't know the specific implementation, but this gave me the inspiration to use multi-threading: using thread asynchronous Implement fast failover and timeout control.

When we use curl to request an address, we can set curl's connection timeout and read data timeout respectively through the CURLOPT_CONNECTTIMEOUT / CURLOPT_TIMEOUT parameters, but the total timeout is difficult to control. Moreover, the timeout period cannot be set when performing database queries (Niao Ge’s blog: Setting query timeout for MySQL).

At this time, we can use multi-threading to implement this function: after executing the start() method of the thread class, do not call the join() method, so that the thread remains in an asynchronous state and does not block the execution of the main thread. .

At this time, the main thread is equivalent to the flagship, and each sub-thread is equivalent to the cruiser. After the flagship arrives at a certain place, it is not necessary to wait for the cruiser to return. It can just wait for a period of time and then leave, thus avoiding accidents of the cruiser. When the flagship is in vain and so on.

Code:

$chG = new Request("www.google.com");
$chB = new Request("www.baidu.com");
$chG->start();
$chB->start();
$chB->join();
// 此处不对chG执行join方法
sleep(1); // sleep一个能接受的超时时间
$gl = $chG->response;
$bd = $chB->response;
$bd->kill();
if (!$gl) {
    $gl = ""; // 处理异常,或在线程类内给$gl一个默认值
}
Copy after login

Summary

PHP’s sealing (yan) installation (ge) of multi-threading makes it very frustrating to use threads. Although it is safe and maintains the simple and easy-to-use style of PHP, it cannot fully utilize the multi-threading capabilities. However, each language has its own characteristics and emphasis, so there is no need to force it. If you love her, you must tolerate her =_=.

Related recommendations:

php asynchronous multi-threaded swoole usage example

A case of implementing PHP multi-threading class

PHP multi-threading small case

The above is the detailed content of PHP's support and use of multi-threaded programming. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles