10-Asynchronous programming (concurrent, parallel, synchronous, asynchronous, blocking, non-blocking), synchronous blocking, asynchronous blocking, synchronous non-blocking, asynchronous non-blocking

1 concurrent
2 parallel
3 sync
4 asynchronous
5 blocking
6 non-blocking
6.1 Synchronous blocking, asynchronous blocking, synchronous non-blocking, asynchronous non-blocking
7 Asynchronous Programming

1 concurrent

1 Concurrency describes the organizational structure of the program. It means that the program should be designed into multiple subtasks that can be executed independently.
2 The purpose is to utilize limited computer resources so that multiple tasks can be executed in real-time or near real-time.
3 The so-called concurrency is to reasonably allocate CPU resources to multiple tasks through an algorithm. When a task performs an I/O operation,
The CPU can switch to other tasks until the I/O operation is completed, or when a new task encounters an I/O operation.
The CPU returns to the original task and continues execution.

4 Although the CPU can only execute one task at the same time, by allocating the CPU usage rights to different tasks at the appropriate time,
Make multiple tasks visually appear to be executed together. The execution speed of the CPU is extremely fast, and the time for switching between multiple tasks is also extremely short.
Users can’t feel it at all, so concurrent execution looks just like the real thing.

2 parallel

1 Parallel describes the execution status of the program. Refers to multiple tasks being executed at the same time.
2 The purpose is to use surplus computing resources (multi-core CPU) to accelerate the completion of multiple tasks.
3 Each core of a multi-core CPU can perform a task independently, and multiple cores will not interfere with each other.
Multiple tasks executed on different cores are truly running at the same time. This state is called parallelism.

'''
Parallelism means two or more events occur at the same time
Concurrency is when two or more events occur at the same time interval
?
For single-core CPUs, concurrency refers to the ability of the CPU to alternately perform different tasks;
For multi-core CPUs, parallelism refers to the ability of multiple cores to perform multiple tasks simultaneously.
?
Single-core CPUs can only be concurrent, not parallel; in other words, parallelism can only occur in multi-core CPUs.
In multi-core CPUs, concurrency and parallelism generally exist at the same time. They are both important means to improve the CPU's ability to handle tasks.
'''

3 sync

1 In order to complete a certain task, different program units need to rely on some kind of communication method to "coordinate" during the execution process. These program units are said to be executed synchronously.

2 For example, when updating product inventory in a shopping system, a "lock" needs to be used as a communication signal to force different update requests to be queued and executed sequentially. Then the operation of updating the inventory is synchronous.

3 Synchronization is to do one thing one by one; the next task will not be executed until the previous task is executed. In short, 'synchronized means ordered'.

4 asynchronous

1 In order to complete a certain task, different program units can complete the task without communication and coordination in the process.
2 Unrelated program units can be asynchronous.

3 For example, a crawler downloads a web page. After the scheduler calls the downloader, other tasks can be scheduled.
There is no need to maintain communication with the download task to coordinate behavior. The downloading, saving and other operations of different web pages are irrelevant, and there is no need to notify each other and coordinate.
The completion time of these asynchronous operations is not determined.

4. When a task has been executed, you can switch to another task without waiting for the task to complete. Simply put, 'asynchronous means out of order'.

The "communication methods" mentioned above usually refer to the synchronization primitives provided by asynchronous and concurrent programming, such as semaphores, locks, synchronization queues, etc.
We need to know that although these communication methods are designed to allow multiple programs to execute synchronously under certain conditions, precisely because of their asynchronous existence,
These means of communication are needed. If all programs are executed in sequence and are themselves synchronous, why are these synchronization signals needed?

5 blocking

1 The state in which the program is suspended when it does not obtain the required computing resources.
2 'While the program is waiting for an operation to complete, it is unable to continue doing other things, then the program is said to be blocked on the operation. '
3 Common forms of blocking include: network I/O blocking, disk I/O blocking, user input blocking, etc.

Blocking is everywhere, including when the CPU switches context, all processes cannot actually do anything, and they will also be blocked.
(If it is a multi-core CPU, the core that is performing the context switch operation cannot be utilized.)

6 non-blocking

1 'While the program is waiting for an operation, if it is not blocked and can continue to run and do other things, the program is said to be non-blocking for the operation. '
2 Non-blocking does not exist at any program level and under any circumstances.
3 A non-blocking state is possible only if the level of program encapsulation can include independent subroutine units.

Non-blocking exists because blocking exists. It is precisely because of the time-consuming and inefficiency caused by blocking a certain operation that we have to make it non-blocking.

To support concurrency, it must be split into multiple tasks. Different tasks are relatively blocking/non-blocking, synchronous/asynchronous.
Therefore, the three words concurrency, asynchronous, and non-blocking always go hand in hand.

6.1 Synchronous blocking, asynchronous blocking, synchronous non-blocking, asynchronous non-blocking

These terms describe different I/O models that involve how applications interact with the system kernel when performing input and output operations.
Here is an explanation of these terms:

1. **Synchronous Blocking:**
   - In the synchronous blocking model, the application initiates an I/O operation and then waits until the operation completes.
   During this waiting process, the application is blocked and unable to perform other tasks.

2. **Asynchronous Blocking:**
   - In the asynchronous blocking model, the application initiates an I/O operation and can immediately perform other tasks.
   The system will notify the application when the I/O operation is completed. At this time, the application needs to block and wait for the notification, and then process the results of the I/O.

3. **Synchronous Non-blocking:**
   - In the synchronous non-blocking model, the application initiates an I/O operation and then returns control immediately instead of waiting for the operation to complete.
   The application needs to periodically check whether the operation is complete, process the results if it is complete, and otherwise continue to perform other tasks.

4. **Asynchronous Non-blocking:**
   - In the asynchronous non-blocking model, the application initiates an I/O operation and registers a callback function or handles it through event notification
   Operation completion event. The application can continue to perform other tasks without periodically checking the status of the operation.
   Once the I/O operation is completed, the system will call the registered callback function or trigger an event notification to notify the application of the processing results.

Generally speaking, synchronous and asynchronous focus on the message communication mechanism, while blocking and non-blocking focus on the program waiting for the call result (message, return value)
status at the time. Synchronous usually means waiting for the call to complete, while asynchronous usually means not waiting for the call to complete. Blocking usually means that the call will continue
Waiting instead of blocking usually means that the call does not wait forever but returns immediately.

7 Asynchronous Programming

'''
Taking processes, threads, coroutines, and functions/methods as the basic units for executing task programs, combined with mechanisms such as callbacks, event loops, and semaphores,
A programming method that improves the overall execution efficiency and concurrency capabilities of the program.
'''

If when a program is running, it can accurately determine which specific operation it will perform next based on the instructions that have been executed,
Then it is a synchronous program, otherwise it is an asynchronous program. (The difference between disorder and order)

Synchronous/asynchronous, blocking/non-blocking are not incompatible, it depends on the encapsulation level of the program in question.
For example, a shopping program can be asynchronous when processing browsing requests from multiple users, but it must be synchronous when updating inventory.

1 Concurrency Parallel

Concurrency: The ability to perform multiple tasks within the same time period. All operating systems support concurrency, and single-core CPUs can also perform concurrency.
Parallelism: The ability to execute multiple tasks at the same time. Parallelism must be supported by multiple CPUs.

Python enables multi-threading. Even if there are multi-core CPUs, it can only be concurrent and cannot be parallel---》The reason is that there is a GIL lock.
Python enables parallelization by enabling multi-process + multi-core CPU


IO-intensive uses multi-threading (io does not consume CPU), and computing-intensive uses multi-process---"What is the reason?

2 Synchronous Asynchronous

Angle of program call
   Synchronization: Synchronization is doing one thing one at a time; the next task will not be executed until the previous task is executed. Synchronicity means orderly
   Asynchronous: When a task has been (started) executed, you can switch to another task without waiting for the task to complete. Asynchronous means out of order
    
    # The asynchronous we used:
    -With the help of other frameworks: scrapy, celery
        -fastapi: asynchronous web framework
        -sanic: asynchronous web framework

3 Blocking Non-blocking

Perspective of program execution
Blocking: While the program is waiting for an operation to complete, it is unable to continue doing other things, then the program is said to be blocked on the operation.
Non-blocking: When the program is waiting for an operation, it is not blocked and can continue to run and do other things. The program is said to be non-blocking for the operation.

I asked a classmate to get me water

I am the caller----"I told him, go get me water--"I have been doing nothing, waiting for him---"Until he comes back, I have been waiting---" Synchronous call
I am the caller---》I told him, go get me water---》I am typing code again---》Until he comes back, I am lecturing, and talk to it again---》Asynchronous call



From an execution perspective---》The classmate ran to get the water---》He has been waiting for the water to be finished---》Turn off the water dispenser---》Come back with the water---》Blocked
From an execution perspective--"The classmate ran to pick up the water--"During the process of picking up the water, it was chatting--"Wait until the water is finished, turn off the water dispenser--"Get the water back--"Non-blocking


There must be io operations before blocking and non-blocking are involved.

synchronous blocking
Synchronous non-blocking
asynchronous blocking
Asynchronous non-blocking