Question 1: How does a computer execute program instructions?
Question 2: How does a computer achieve concurrency?
Question 3: What does true parallelism depend on?
Parallel implementation of multiple processes
1/ What is process?
Computer programs are files stored on disk (note disks here) and only when they are loaded into memory and called by the operating system do they have a life cycle of their own. A process represents an executing program. Each Process has its own address space and other secondary processes are instances of running programs in the computerCopy the code
2/ How do I use processes in Python?
import multiprocessing # Import process module
import datetime
import time
def func(data) :
while True:
print(datetime.datetime.now())
sum = data + 100
print(sum)
time.sleep(2)
print(datetime.datetime.now())
if __name__ == '__main__':
Instantiate an object, create a process, args pass the parameter, must be a tuple
p = multiprocessing.Process(target=func,args=(123,))
p.start() # run thread p
while True:
time.sleep(2)
print("I'm the master process.")
Copy the code
Output from the above code:Copy the code
The process usage steps are as follows:Copy the code
3/ What are the necessary conditions for multiple processes to achieve parallelism?
Total number of processes <= number of CPU cores If not, then the program running is an illusion generated by polling scheduling.Copy the code
Multithreading concurrency
1/ What is a thread?
Threads are called lightweight processes. A thread is an entity in a process. The operating system does not allocate memory for a process. It has only a few resources that are essential for running. Multiple threads within the same process share the same context, that is, resources (memory and data). Thread: the smallest unit in which an operating system can schedule operationsCopy the code
2/ How do I use threads in Python?
import multiprocessing # reference process module
import threading # reference thread module
import time
def func(data) :
while True:
time.sleep(1)
data += 1
print(data)
# mult = multiprocessing.Process(target=func, args=(1314,))
# mult.start() # run the process
thre = threading.Thread(target=func, args=(500)),Create a thread
thre.start() # run thread
print("This is the main process.")
Copy the code
The result of the above code is as follows:Copy the code
Thread usage steps are shown below:Copy the code
3/ Why is multithreading not parallel?
Stability:Copy the code
Processes have an independent address space. The crash of one process does not affect other processes. Threads share the address space, and when one thread fails to manipulate the shared data illegally, the entire process crashes. Creation overhead: The operating system allocates memory and other resources to create processes, which is expensive. By creating threads, the operating system does not need to allocate resources separately and costs less. Switching overhead different processes are directly independent, and switching requires large resource consumption of threads to share process address space, and switching overhead is small
GIL lock
When Python was designed, there was no concept of a multi-core processor. Therefore, for design convenience and thread safety, a lock is directly designed. This lock requires that only one thread in any process be executing at a time. Therefore, it is not possible to allocate multiple cpus for multiple threads. So threads in Python can only do concurrency, not true parallelism. But the GIL lock in Python3 has a nice design that automatically switches threads in case of blocking (not time-consuming). Many libraries are written based on the GIL lock, which is too expensive to cancel. Processes can achieve concurrency and parallel threads can only achieve concurrency, and automatically switch when blocked. We can use this mechanism to make full use of the CPU.Copy the code
Key points for implementing concurrent servers using multi-processes and multi-threads Key point 1: Multi-processes are executed in parallel, which means that each request is handled independently. Key point 2: Multithreading, while not running in parallel, can achieve the effect of concurrency by avoiding blocking and switching threads without wasting CPUCopy the code