


The difference between blocking and non-blocking PHP programs is: before the blocking call result is returned, the current thread will be suspended, and the caller will not continue to execute; before the non-blocking call result is returned, the call will not Block the current thread and continue execution.
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
What is the difference between blocking and non-blocking PHP programs?
Blocking and non-blocking focus on the status of the program while waiting for the call result (message, return value).
A blocking call means that the current thread will be suspended before the call result is returned. The calling thread will return only after getting the result.
Non-blocking call means that the call will not block the current thread until the result cannot be obtained immediately.
1.php concurrency blocking
Concurrency is a problem for PHP. We often encounter some queries at work to determine whether the data exists before writing the database. But if there is concurrency, it will cause data to be written repeatedly, and your judgment will become false. So with blocking, we need to execute the requests one by one.
Brief introduction:
1. First, open or create the file lock.txt file in read and write mode
2. Apply "exclusive lock" to the lock.txt file, and After the lock is successful, you can proceed to the next step of "processing the order product data"
3. After processing the data, you must "release the lock" and fclose the open file
Note: Give the file After "exclusive lock", if there is no "release lock" inside, there will be a very stuck situation
public function index(){ $fp = fopen("lock.txt", "w+"); if(flock($fp,LOCK_EX)) { $find=Db::name('user')->where('username','name2')->find(); if($find){ $data['username']='name3'; $data['password']=''; $data['password_m']=''; Db::name('user')->insert($data); }else{ $data['username']='name2'; $data['password']=''; $data['password_m']=''; Db::name('user')->insert($data); } flock($fp,LOCK_UN); } fclose($fp); return 'success'; }
Note: It is feasible for small concurrency, and the performance will not have a big impact. It is better if the concurrency is less than 500. If it is too high, it is recommended to use queue mode.
2.php non-blocking mode
Non-blocking mode is commonly used in php programs to call third-party api interfaces, or programs that do not need to wait for results.
Give a simple example. To send emails, you need to send them to all your bosses, so you need to send them all once. Maybe you have to write a loop, so the method of sending emails in this loop requires execution time, and you need to get the return value and execute it next time. Loop, this time is accumulated sequentially, and the final time will be very long.
Then this is called blocking, and what we always want is to submit the past without waiting for the return data, and you will loop Just run it once, then what we want is non-blocking mode. For this kind of situation, PHP doesn't have any good functions to handle it, so what we do most is to queue it up. When sending emails,
executes according to the queue, but in fact, the blocking mode is still used. But our request execution time will be compressed very short. The most commonly used one is to call the API interface, and you don't care about the return value at this time.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between blocking and non-blocking PHP programs?. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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.

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

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

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software