Introduction to Process class parameters
Group--------parameter is not used and the value is always none
The target--------represents the calling object, which is the task to be performed by the child process
The args----------represents the positional parameter tuple of the calling object, args= ("Hades ',") is a tuple form and must have a comma
Kwargs-------represents the dictionary of the calling object, kwargs={' name ': ' Hades ', ' Age ': 18}
Name---------as child process
Introduction to Process class methods
P.start ()-----------------start the process and call P.run () in the child process
P.run ()-------------------the method that runs at the start of the process, it is necessary to implement the method in the class of the custom class by invoking the function specified by the target.
P.terminate ()-----------forcibly terminates the process P, does not perform any cleanup operations, and if p creates a child process, the subprocess becomes a zombie process, which requires special care in this case.
If P also holds a lock then it will not be released, resulting in a deadlock
P.is_alive ()--------------Determine if the process is alive, if P is still running, returns true
P.join ()-------------------The main process waits for the P child process to terminate (the state in which the main process is in progress, while the P child process is in the running state), can only join the START process, and cannot join the run-open process
Introduction to the Process class properties
P.daemon---------------The default value is False, if set to True, represents the daemon that P is running in the background, when P's parent process terminates, p also terminates with it, and when set to True, p cannot create its own new process, it must be in P.start () Before setting
P.name the name of the------------------process
PID of P.pid---------------------process
P.exitcode--------------process is none at run time, if it is-N, the signal N is ended (knowledge points are known)
P.authkey the authentication key for the---------------process, which by default is a 32-character string that is randomly generated by os.urandom ().
The purpose of this key is to provide security for the underlying interprocess communication that involves a network connection, which can only succeed if you have the same authentication key (learn about the knowledge points)
What is a process
Using processes is to implement concurrency effects
A process is an ongoing/running program, in other words, a process is a process that is run by a program
Program vs Process:
Program: Just a bunch of code files
Process: The process of running a program is the process
Serial: A task is completed and completed, followed by the next task, in order
Determine the concept of serial:
The serial appears to be a running one:
One operation is serial: Error
Concurrency (toggle + save state): Multiple tasks appear to be running concurrently, and a single core can be implemented concurrently
Parallel: Multiple tasks are really running simultaneously, only multicore can be implemented in parallel
The background of multi-channel technology: To achieve concurrency in a single core
How to achieve:
1. Space reuse: The memory is divided into several parts, each part into a program of data, so that the same time in memory has a multi-channel program data, for the CPU to switch between multiple tasks to prepare
2. Time reuse: Time for multiple processes to share CPUs
The key point is that the CPU switches between multiple tasks
There are two scenarios in which switching occurs:
1. One task takes too long CPU time (no IO operation): Reduces efficiency
2. A task encountered IO operation during operation: can improve efficiency
How to turn on child processes 1
1 fromMultiprocessingImportProcess2 Import Time3 4 5 defTask (name):6 Print('%s is running'%name)7Time.sleep (3)8 Print('%s is done'%name)9 Ten One #The operation to open a child process on a Windows system must be placed under that line of code A if __name__=='__main__': -p = Process (Target=task, args= ('Child Process',))#process (target=task,kwargs={' name ': ' Subprocess '}) # -P.start ()#Just sending a signal to the operating system to create a subprocess. the Print('Master')
Terminal printing:
How to turn on child processes 2
1 fromMultiprocessingImportProcess2 Import Time3 4 5 classmyprocess (Process):6 def __init__(self, name):7Super ().__init__()8Self.name =name9 Ten defRun (self): One Print('%s is running'%self.name) ATime.sleep (3) - Print('%s is done'%self.name) - the - #The operation to open a child process on a Windows system must be placed under that line of code - if __name__=='__main__': -p = myprocess ('Child Process') +P.start ()#Just sending a signal to the operating system to create a subprocess. - P.join () + Print('Master')
Terminal printing:
Join method
1 fromMultiprocessingImportProcess2 Import Time3 4 5 defTask (name, N):6 Print('%s is running'%name)7 time.sleep (n)8 Print('%s is done'%name)9 Ten One if __name__=='__main__': AStart =time.time () -p_l = [] - forIinchRange (1, 4): thep = Process (Target=task, args= ('Child process%s'%I, i)) - p_l.append (P) - P.start () - + forPinchp_l: - P.join () +Stop =time.time () A Print('Master', (Stop-start))
Terminal printing:
Memory isolation between processes
1 fromMultiprocessingImportProcess2 3n = 1004 5 6 deftask ():7 GlobalN8n =09 Ten One if __name__=='__main__': Ap = Process (target=task,) - P.start () - P.join () the Print('Master', N)
Terminal printing:
PID of the process
1 fromMultiprocessingImportProcess, Current_process2 ImportTime , Os3 4 5 deftask ():6 Print('child process [%s] parent process <%s>'%(Os.getpid (), Os.getppid ()))7Time.sleep (300)8 9 Ten if __name__=='__main__': Onep = Process (target=Task) A P.start () - Print('Master', Os.getpid (), Os.getppid ()) -Time.sleep (1000)
Terminal printing:
Process object Other related properties or methods
P.name changing the name of a child process
1 fromMultiprocessingImportProcess2 ImportTime , Os3 4 5 deftask ():6 Print('%s is running'%os.getppid ())7Time.sleep (300)8 9 Ten if __name__=='__main__': Onep = Process (Target=task, name='Sub-Process 1') A P.start () - Print(p.name) - Print('Master') theTime.sleep (1000)
Terminal printing:
Terminate and Is_alive
P.terminate () Close process
P.is_alive () to see if the process is alive
1 fromMultiprocessingImportProcess2 ImportTime , Os3 4 5 deftask ():6 Print('%s is running'%os.getppid ())7Time.sleep (300)8 9 Ten if __name__=='__main__': Onep = Process (Target=task, name='Sub-Process 1') A P.start () - p.terminate () -Time.sleep (0.1) the Print(P.is_alive ()) - Print('Master')
Terminal printing:
Python Concurrent Programming-process