Main content:
1 noun explanation
Parallel: Two or more things that execute simultaneously at the same point in time. is from the microscopic, that is, at a precise moment, there are different programs in execution, which requires that there must be multiple processors
Concurrency: That is, two things or multiple things alternating at the same time period. Is from the macro, in a time period can be seen in parallel execution, such as a server processing multiple sessions simultaneously.
Synchronization: The so-called synchronization is the completion of a task needs to rely on another task, only wait for the task to be relied upon to complete, the dependent task can be counted, this is a reliable task sequence . Either success succeeds, failure fails, and the state of the two tasks remains the same.
Async: The so-called async is no need to wait for the task to be relied upon to complete, just notify the dependent task to complete what work, dependent on the task is also immediately executed, as long as the completion of their own complete task, even if completed . As to whether the dependent task is ultimately completed, the task that relies on it cannot be determined, so it is an unreliable task sequence.
Blocking:
Non-blocking:
2. Open process two ways
1) The first kind
From multiprocessing import Processimport Osimport timedef func (i): time.sleep (0.2) print (' This is a child process%s,pid is%s, The PID of its parent process is%s '% (I, os.getpid (), Os.getppid ()))# Os.getpid (), obtains the current process's pid,# os.getppid (), obtains the PIDif __ of the current process's parent process name__ = = ' __main__ ': for I in range (2): p = Process (Target=func, args= (i)) #实例化一个进程对象 #target: The task to be performed by the child process, args: The parameter that the parent process will pass to the child process must be in the Ganso form. P.start () #开启一个进程. Time.sleep (0.2) print (' This is the parent process, PID is%s, the PID of its parent process is%s '% (Os.getpid (), Os.getppid ()))
2) Inheritance
From multiprocessing import Processimport timeimport osclass myprocess (Process): def __init__ (self): super ( Myprocess, self). __init__ () def run (self): print (' Create child process in class ') if __name__ = = ' __main__ ': p = myprocess () P.start () #是指解释器告诉操作系统, go help me open a process, ready state P.run () # The interpreter tells the operating system to help me perform this process right away. Execution status
With a name.
From multiprocessing import Processimport timeimport osclass myprocess (Process): def __init__ (self, name): Self.name = name super (myprocess, self). __init__ (name = name) If you do not write __init in the parent class, the name will be overwritten. def run: print (' This is a child process that is opened as a class, with the name%s '% self.name) if __name__ = = ' __main__ ': p = myprocess (' Alex ') P.start () #是means the interpreter tells the operating system to help me open a process, ready state. # P.run () The interpreter tells the operating system to open a process and execute the state immediately.
3. Common ways to process:
1) Start and join
Join: allows the main process to wait for the child process to finish executing
From multiprocessing import Processimport Osimport timedef func (): For i in range: time.sleep (0.1) Print (' This is a child process ') if __name__ = = ' __main__ ': p = Process (Target=func) #子进程要执行的任务 P.start () # P.join () #是让主进程等待子进程执行完, Phenomenon: The main process goes to this sentence, the main process is blocked, waiting for the child process to finish executing. For I in range: time.sleep (0.1) starts a normal child process, the parent process waits for the child process to end, the parent process is the program to end # p.join () # is to let the main process wait for the child process to finish executing. How do I change the relationship between the parent and child processes to be synchronous or asynchronous? join must be placed behind start ()
2) Is_alive and terminate
def func (): time.sleep (1) if __name__ = = ' __main__ ': p = Process (Target=func,) P.start () kills P processes, Let the interpreter tell the operating system, please kill the P process. Print (' is the child process still alive? ', P.is_alive ()) time.sleep (0.002) print (' is the child process still alive? ', P.is_alive ()) Returns a bool value that, if true, represents the process is still alive, and if it returns false, the child process dies p.is_alive () to determine if the P process is still alive # P.terminate () Kill P process
4. Common properties of the process:
1) name and PID
From multiprocessing import Processimport Timeimport osdef func (): print (' This is a child process, PID is%s '% os.getpid ()) If __name__ = = ' __main__ ': p = Process (Target=func) P.start () p.name = ' Alex ' print (' can see the PID of the subprocess ', p.pid print (' Can see the child process's name ', p.name) print (' child process is not daemon ', P.daemon) false
2) Daemon Process
Feature: Set the process to daemon and must be daemon = False before start
The daemon will end with the end of the main process
Daemons cannot have child processes.
* * Daemon will end with the end of the main process
From multiprocessing import Processimport timedef func (): time.sleep (3.1) print (' This is son process ') if __name __ = = ' __main__ ': p = Process (target=func) P.daemon = True #必须在start之前设置 p.start () time.sleep (3) the print (' This is parent process ') # daemon will end with the end of the main process.
* * Daemons cannot have child processes
From multiprocessing import Processimport timedef func1 (): print (' Here is grandson ') def func (): p = Process (TARGET=FUNC1) P.start () time.sleep (5) print (' Here's son oh ') if __name__ = = ' __main__ ': p = Process (Target=func) P.daemon = True # Sets the P process to daemon and must be set before start P.start () time.sleep (1) print (' This is Papa ')# daemon: Do not allow child processes to be opened
35 two ways to open parallel/outgoing/asynchronous non/blocking processes, common methods and properties of processes