Asynchronous, synchronous, blocking, non blocking

Time:2021-4-6
  • Synchronization means that when the completion of a task depends on another task, the dependent task can only be completed after waiting for the dependent task to complete. This is a reliable task sequence.

  • Asynchrony does not need to wait for the dependent task to complete, it just tells the dependent task what to complete, and the dependent task will be executed immediately, as long as it completes the whole task, it will be completed.

  • Blocking call means that before the call result returns, the current thread will be suspended, waiting for message notification and unable to perform other business.

  • A non blocking call means that the function does not block the current thread and returns immediately before it can get the result immediately.


Asynchronism and synchronization occur in the user space. When the user initiates an IO call and synchronizes, if the operation is time-consuming, the following process will be blocked.

file_get_contents("http://www.qq.com/");
echo "end";

When calling the read operation. The next operation echo will wait for the above result to complete before continuing.

This is aysnc_ Read is an asynchronous read operation. When reading, the next operation will not block and will output end first. When the data arrives, echo $data again;

aysnc_read("http://www.qq.com",function($data){
    echo $data;
})
echo "end";

Blocking, non blocking, occurs between the kernel and user space.

Blocking means that the operating system will suspend the process until the data is ready, and the current process can continue to execute without blocking or blocking by the operating system.

Blocking IO

Zhang San went to the bookstore to buy a book, and then asked the boss if there was a dream of Red Mansions. The boss said that I would check. The time of this inquiry was relatively long. Then Zhang San couldn’t do anything and was waiting. Until the boss told it to find it. Then he bought the book and left. Zhang San’s operations are all synchronous blocking. You must wait for the boss’s result before the following operations can be executed.

non-blocking io

Or Zhang San to buy books, the boss to inquire. It’s time for Zhang San to play with his mobile phone, and then ask every other time, have you found it? Zhang San’s process is not blocked. But the task is synchronous and must wait for the result. That is, the boss didn’t tell Zhang San the result. Zhang San can’t leave to do other things. This process is synchronous and non blocking.

Asynchronous IO

Zhang San went to buy books. Then go to the bookstore and ask the boss if he has any. The boss needs to inquire. Zhang San tells the boss his mobile phone number, calls me when he finds it, and then goes to do other things. The process is asynchronous. Zhang San’s process has not been blocked in this link of buying books. This is asynchronous nonblocking.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]