Process theory (book: now operating System)
A process is a running program or a program that runs a process.
PID Process ID progress in the operating system of the ID number
Serial, concurrent, parallel, multi-channel technology
The nature of concurrent implementations: Toggle + Save State
Time multiplexing: Taking CPU too long or having another higher priority task
i/0 operation Read/write
Time.sleep () Nature and time multiplexing the operating system switches the CPU as it sleeps.
Eight cores: eight CPUs
Multi-core Assignment: 5 tasks, quad cores, one CPU first, then I/O or a program takes too long
Switch to a new task, and then I/O executes, switching to any idle CPU.
Save the progress when switching, the single core can continue to cut back to continue to run the program
The same program executes two times and also opens two processes.
Two ways to open sub-process
Os.cpu_count () See how many processors the computer is
The multiprocessing module is used to open sub-processes and perform our custom tasks (such as functions) in the subprocess, which is similar to the programming interface of the Multithreaded module threading.
Unlike threads, processes do not have any shared state, and process-modified data is limited to that process.
Child process implementation mode one (with comments)
Way one is what we used to do
## # Way One: fromMultiprocessingImportProcessImport TimedefTask (x):Print('%s is running'%x) time.sleep (3) Print('%s is done'%x)if __name__=='__main__': #process (target=task,kwargs={' x ': ' subprocess '})P=process (target=task,args= ('Child Process',))#if args= (), there is only one parameter inside the parentheses, be sure to remember to add commas #target represents the calling object, the task that the child process is to perform, and args is the positional parameter tuple of the calling object. P.start ()#just when the operating system sends a signal to turn on the subprocess, the operating system will apply to open up a memory space. #The data of the parent process is then copied to the child process as the initial state of the child process. Print('Master')
Implementation Method One
Sub-process implementation mode two
#Way Two: fromMultiprocessingImportProcessImport Timeclassmyprocess (Process):def __init__(self,x): Super ().__init__() Self.name=xdefRun (self):Print('%s is running'%self.name) Time.sleep (3) Print('%s is done'%self.name)if __name__=='__main__': P=myprocess ('Sub-Process 1') P.start ()#P.run () # The custom class sends a signal to the operating system to turn on the subprocess, and the operating system will request a memory space. #The data of the parent process is then copied to the child process as the initial state of the child process. Print('Master')
child process implementation mode two
Open multiple sub-processes at the same time
fromMultiprocessingImportProcessImport TimedefTask (x,n):Print('%s is running'%x) time.sleep (n)Print('%s is done'%x)if __name__=='__main__':#multiple child processes can be opened #process (target=task,kwargs={' x ': ' subprocess '})P1 = Process (Target=task, args= ('Sub-Process 1', 3))#if args= (), there is only one parameter inside the parentheses, be sure to remember to add commasP2 = Process (Target=task, args= ('Sub-process 2', 5))#if args= (), there is only one parameter inside the parentheses, be sure to remember to add commasP1.start ()#just send a signal to the operating system to turn on the child processP2.start ()#just send a signal to the operating system to turn on the child process Print('Master')
multiple child processes open at the same time
Process theory and two ways to turn on sub-processes