The incarnation of the computer-multi-process

In the previous blog, we talked about multitasking in computers, and in Python programs, you can use processes to achieve multitasking. Processes are a way to achieve multitasking.

process

Process is a running activity of a computer program on a certain data set. It is the basic unit of the system for resource allocation and scheduling. It is the foundation of the operating system structure. In the early process-oriented computer structure, the process is the program The basic execution entity; in contemporary thread-oriented computer architecture, the process is the container of the thread, the program is the description of instructions, data and its organization, and the process is the entity of the program. In
general: the process is an instance of the running program
It is like having a large factory (computer), and there are production workshops (processes) in the factory, and there are employees (threads) on the production line in the workshop, and the factory provides the required resources (memory, run space ...)
a program running at least one process, a process has a default threads, processes which can create multiple threads, lines Cheng is attached to the inside of the process, no process is no thread

Features of the process:

Dynamic : A process is an execution process of a program, it is temporary, has a lifetime, is dynamically generated, and dynamically dies.
Concurrency : Any process can execute concurrently with other processes.
Independence : A process is a system that allocates resources and An independent unit of scheduling
structure : the process is composed of three parts: program, data and process control block

The process can be subdivided into: single process and multiple processes

Single process

The default program running creates a process and a Python file to run, which is to start a process to handle the scene in the process: the main thread executes the code

multi-Progress

A Python file runs and occupies a process to process. If you want to run a second Python file at the same time, you can also open a process for the second Python file to process multiple processes to complete multiple tasks.
Each process is like an independent workshop. Each workshop is operating separately, and each process is also operating separately, performing its own tasks

Multi-process use

Before using multi-process, you must first import the package

import multiprocessing

When using, first create a process.
Use the .Process method, and open the process after creation. Use the .start method
Process([group,target,name,args,kwargs]) Process class construction
1.group: Specify the process Group, currently you can only use None
2.target: the name of the target task to be executed
3.name: the name of the process
4.args: pass parameters to the executed task in the form of meta-ancestors
5.kwargs: pass parameters to the executed task in the dictionary method
Process The commonly used method of the created instance object
start(): start the child process instance (start to create the child process)
join(): wait for the end of the child process execution, which is equivalent to a separate detection function for the process, when the child process is executed After that, the main process continues to execute
terminate(): regardless of whether the task is completed, immediately terminate
the instance object created by the child process. Commonly used attributes
name: the alias of the current process, the default is Process-N, N is an increasing integer starting from 1
The kill method in Process
Insert picture description here

Process characteristics

1. Do not share global variables
When a process modifies global variables, it will not cause any impact on other processes. It can be understood that each process takes the initial global variables. Or it can be understood that global variables are so-called resources. When a process is created, the system will directly copy a global variable to the process. For this global variable, the processes are independent of each other, and there is no connection between them
. 2. The main process ends
when all the child processes have ended. If the main process needs to be ended, the entire program ends:
1 Before the end of the main process, ensure that all sub-processes terminate using the sub-process's terminate()
2. Before the sub-process is opened, set the current sub-process to be guarded by the main process, and the sub-process's demoon attribute is set to true, which means this sub-process The process is guarded by the main process, the main process ends, the guard ends, and the child process ends

Don't understand??? Don't worry, just go to talent!!!

Case code

Use segment process to achieve simultaneous upload and download

import time
import multiprocessing


def upload():
    for i in range(11):
        print(f"当前上传进度为:{i * 10}%......")
        if i == 10:
            print("上传完成!")
        time.sleep(1)


def downloda():
    for i in range(11):
        print(f"当前下载进度为:{i * 10}%......")
        if i == 10:
            print("下载完成!")
        time.sleep(1)

print("1.上传")
print("2.下载")
print("3.退出程序")

while True:
    up_process = multiprocessing.Process(target=upload)
    down_process = multiprocessing.Process(target=downloda)
    chose = input("请输入想要进行的操作:")
    if chose == "1":
        up_process.start()
        continue
    elif chose == "2":
        down_process.start()
        continue
    elif chose == "3":
        print("退出程序成功,欢迎下次使用!")
        break
    else :
        print("请输入正确的序号!")

Guess you like

Origin blog.csdn.net/Layfolk_XK/article/details/107975163