Home  >  Article  >  Backend Development  >  What is the difference between blocking and non-blocking PHP programs?

What is the difference between blocking and non-blocking PHP programs?

WBOY
WBOYOriginal
2022-03-18 12:18:092392browse

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.

What is the difference between blocking and non-blocking PHP programs?

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!

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