Concept
Concurrent programming Is to make the program execute multiple tasks at the same time , How to implement concurrent programming , This is about process and Threads These two concepts .
For the operating system , A task ( Or the program ) It's a process (Process), For example, opening a browser is to start a browser process , Opening wechat starts a wechat process , Open two notepads , Just start two Notepad processes .
The characteristics of the process are :
-
operating system In progress Allocate storage space , Each process has its own address space 、 The data stack And other auxiliary data used to track process execution ;
-
The process can be
fork
perhapsspawn
Way to create a new process to perform other tasks -
Each process has its own independent memory space , So the process needs to pass Interprocess communication mechanism (IPC,Inter-Process Communication) To achieve data sharing , Specific ways include The Conduit 、 The signal 、 Socket 、 Shared memory area etc.
A process can also do many things at the same time , For example Word Typing at the same time 、 Pinyin check 、 Printing and so on , That is, a task is divided into multiple sub tasks and carried out at the same time , these Subtasks within a process are called threads (Thread).
Because each process needs to accomplish at least one thing , That is, a process has at least one thread . When you want to implement concurrent programming , That is, when performing multiple tasks at the same time , There are three solutions :
-
Multi process , There is only one thread per process , But multiple processes perform multiple tasks together ;
-
Multithreading , Just start one process , But opening multiple threads in one process ;
-
Multi process + Multithreading , That is, start multiple processes , Each process starts multiple threads , But this method is very complex , It is seldom used in practice
Be careful : real Multitasking can be performed in parallel only in multi-core CPU In order to achieve , Single core CPU In the system , True concurrency is impossible , Because at some point you can get CPU Only one thread , Shared by multiple threads CPU Execution time of .
Python It supports multiple processes and threads at the same time , The following describes multiprocessing and multithreading respectively .
Multi process
stay Unix/Linux
In the system , Provides a fork()
system call , It's a special function , A normal function call is a call to , Go back to , but fork
Function call once , Go back twice , Because the function is called by the parent process , Then copy out a part of the process , Finally, it returns... In both parent and child processes , So it will return twice .
The subprocess always returns 0
, The parent process will return the ID, Because the parent process can copy multiple child processes , So you need to record the of each child process ID, The child process can call getpid()
Gets the ID.
Python in os
Modules encapsulate common system calls , This includes fork
, The code example is as follows :
Running results :
because windows The system does not exist fork
, Therefore, the above function cannot call , but Python It's cross platform , Therefore, there are other modules that can realize the function of multi process , such as multiprocessing
modular .
multiprocess
multiprocessing
Module provides Process
Class to represent a process object , Next, an example of downloading a file is used to illustrate the difference between using multiple processes and not using multiple processes .
The first is the example of not using multiple processes :
The operation results are as follows , Here we use randint
Function to randomly output the time consumption of the currently downloaded file , From the results , The running time of the program is equal to the sum of the task time of two downloaded files .
If you are using multiple processes , Examples are as follows :
In this multi process example , We go through Process
Class creates a process object , adopt target
The parameter is passed into a function to represent the task that the process needs to perform ,args
It's a tuple , Represents the parameters passed to the function , Then use start
To start the process , and join
Method to wait for the end of process execution .
The running results are as follows , Time consumption is not the sum of the execution time of two tasks , The speed is also greatly improved .
Pool
The above example starts two processes , But if you need to start a large number of sub processes , The above code is not written properly , The sub processes should be created in batch by means of process pool , Still use the example of downloading files , But execute the following code as follows :
In the code Pool
Object first creates 5 A process , then apply_async
The method is to start the process and execute the task in parallel , call join()
Method must be called before close
() ,close
() It mainly closes the process pool , So you can't add new process objects after executing this method . then join()
Is to wait for all processes to complete their tasks .
The running results are as follows :
Subprocesses
In most cases , A child process is an external process , Not itself . After the child process is created , We also need to control the input and output of the subprocess .
subprocess
Module allows us to start subprocesses and manage the input and output of subprocesses .
Here is a demonstration of how to use Python Demo command nslookup www.python.org
, The code is as follows :
Running results :
If the child process needs to enter , Can pass communicate()
Input , The code is as follows :
This code is to execute the command nslookup
when , Input :
Running results :
Interprocess communication
Processes need to communicate ,multiprocess
The module also provides Queue
、Pipes
And so on .
Here we use Queue
For example , Create two child processes in the parent process , A to Queue
Write data , The other is from Queue
Reading data . The code is as follows :
The running results are as follows :