Before understanding other concepts, we must first understand several states of the process. During the running of the program, due to the control of the scheduling algorithm of the operating system, the program will enter several states: ready, running and blocked.
Ready (Ready) state: When the process has been allocated all necessary resources except the CPU, it can be executed immediately as long as it obtains the processor. The process state at this time is called the ready state. .
Execution/Running (Running) state When the process has obtained a processor and its program is being executed on the processor, the process state at this time is called the execution state.
When a process that is executing in the Blocked state cannot be executed because it is waiting for an event to occur, it abandons the processor and is in a blocked state. There can be many types of events that cause process blocking, such as waiting for I/O to be completed, application buffer being unsatisfied, waiting for letters (signals), etc.
The so-called asynchronous means that there is no need to wait for the dependent task to complete, but only to notify the dependent task of what work to complete, and the dependent task also Execute immediately, as long as you complete the entire task, it will be completed. As for whether the dependent task is actually completed in the end, the task that depends on it cannot be determined, so it is an unreliable task sequence.
Example
The first way: choose to wait in line;
The second way: choose to take a small note on it I have my number, and when my number is reached, the person at the counter will notify me that it is my turn to handle the business;
The first type: the former (waiting in line) is synchronization Waiting for message notification, that is, I have to wait for the bank's business status;
The second type: the latter (waiting for notification from others) is to wait for message notification asynchronously. In asynchronous message processing, the person waiting for the message notification (in this case, the person waiting to handle the business) often registers a callback mechanism. When the awaited event is triggered, the triggering mechanism (in this case, the person at the counter) passes some kind of callback mechanism. The mechanism (in this case a number written on a small piece of paper, called a number) finds the person waiting for the event.
Example
Continue the above example, whether you are queuing or using a number to wait for notification, if during the waiting process, the waiter If you cannot do anything else except wait for message notification, then the mechanism is blocking, which is reflected in the program, that is, the program has been blocked at the function call and cannot continue to execute.
On the contrary, some people like to make calls and send text messages while waiting while handling these services at the bank. This state is non-blocking because he (the waiter) is not blocked on this message notification. , but wait while doing your own thing.
Note: The synchronous non-blocking form is actually inefficient. Imagine that you are talking on the phone and need to look up to see if the queue is waiting for you. If making a call and observing the queue position are regarded as two operations of the program, the program needs to switch back and forth between these two different behaviors, which is obviously inefficient; the asynchronous non-blocking form is There is no such problem, because making a call is your business (the waiter), and notifying you is the counter's (message triggering mechanism) business, and the program does not switch back and forth between two different operations.
If people waiting to handle business at the bank use an asynchronous method to wait for messages Being triggered (notified) means receiving a small note. If he cannot leave the bank to do other things during this period of time, then it is obvious that this person is blocked in this waiting operation.
Asynchronous operations can be blocked, but they are not blocked while processing messages, but blocked while waiting for message notification.
Imagine that while you are making a phone call, you still need to look up to see if the queue has reached you. If you think of calling and observing the queue position as If there are two operations in the program, the program needs to switch back and forth between these two different behaviors, which is conceivably inefficient.
Because calling is your (waiter)'s business, and notifying you is the counter's (message triggering mechanism)'s business, the program does not have two different Switch back and forth during the operation.
For example, this person suddenly realizes that he is addicted to cigarettes and needs to go out for a smoke, so he tells the lobby manager that when his number is reached, please go outside and notify me, then he will not be Blocking on this waiting operation is naturally an asynchronous non-blocking method.
The above is the detailed content of What are synchronization, asynchronous, blocking and non-blocking in Python?. For more information, please follow other related articles on the PHP Chinese website!