Python tutorial video for beginners to watch online for free
https://space.bilibili.com/3461579964156657
multiprocessing module
The multiprocessing package is a multi-process management package in Python. Similar to threading.Thread, it can use multiprocessing.Proces object to create a process.
This process can run functions written inside Python programs. The Process object has the same usage as the Thread object, and also has the methods of start() and run().
In addition, there are also Lock/Event/Semaphore/Condition classes in the multiprocessing package (these objects can be passed to each process through parameters like multi-threading), which are used to synchronize processes, and their usage is consistent with the class of the same name in the threading package.
Therefore, a large part of multiprocessing uses the same set of APIs as threading, but it is changed to a multi-process situation. Next we learn through a case study:
python学习交流Q群:309488165 ###
import time
import multiprocessing
def download () :
print("开始下载文件...")
time.sleep(1)
print("完成下载文件...")
def upload() :
print("开始上传文件...")
time.sleep(1)
print("完成上传文件...")
#download()
# upload()
#多进程与多线程的使用方式是差不多的
download_process = multiprocessing.Process(target=download)
upload_process = multiprocessing. Process(target=upload)
if _name_ == '_main_':
#多进程必须要在 if _name_ == "_main_”里面
download_process.start()
upload_process.start ()
#默认情况下,主进程代码运行完毕之后会等待子进程结束
print('--主进程运行完了---')
The above code is a very simple program. Once this program is run, according to the execution order of the code, the upload function can only be executed after the download function is executed. If download and upload can be run at the same time, obviously the efficiency of executing this program will be greatly improved.
Main points :
- Process (Process) is the smallest unit of resource allocation
- Multi-processing is a way to achieve multi-tasking in Python programs. Using multi-processing can greatly improve the execution efficiency of programs.
1. Process creation
-
import process package
import multiprocessing
-
Create a process object through the process class
process object = multiprocessing.Process()
-
Start the process to execute the task
process object.start()
Create a process object through the process class
Process object = multiprocessing.Process (target = task name)
2. Process parameter passing
task with parameters
There are two ways for a process to execute a task with parameters:
- Parameter passing in tuple mode: parameter passing in tuple mode must be consistent with the order of parameters.
- Pass parameter in dictionary mode: the key in the dictionary must be consistent with the parameter name.