


What are synchronization, asynchronous, blocking and non-blocking in Python?
1. Status introduction
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.
2. Synchronization and asynchronousness
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.
3. Blocking and non-blocking
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.
4. Synchronous/asynchronous and blocking/non-blocking
1. Asynchronous blocking form
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.
2. Synchronous non-blocking form
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.
3. Asynchronous non-blocking form
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.
