From multiprocessing import process# calls the Process module in multiprocessing import Timeimport os## def fn (i): # time.sleep (1.25) # Print ("This is the pip%s of the child process, the parent process is pip%s"% (Os.getpid (), Os.getppid ())) #获得pid, use Getpid () # # if __name__== "__main__": # for I in RA Nge (2): # p=process (target=fn,args= (i,)) #target是子进程要执行的任务, the args pass a parameter, and pass the value in the form of Ganso # P.start () #开启子进程, the underlying call to the Run function # Print ("This is the pip%s of the child process, the PIP for the parent process is%s"% (Os.getpid (), Os.getppid ())) #并行, simultaneous execution at the same point in time, simultaneous # Synchronization in a period of time, two tasks interdependent, and successful, Need to wait for the last task to be completed before execution, more reliable; asynchronous, simultaneous execution, two tasks executing at the same time on the macro # blocking and non-blocking depends on the state of the Wait message notification. # DEF FN (i): # Time.sleep (2) # print (666) # # if __name_ _== "__main__": # p=process (target=fn,args= (2,)) # # p.daemon=true# After the parent process is terminated, the child process also ends # p.name= "ZMC" # P.start () # # p.terminate () #杀死子进程 # # Time.sleep (0.2) # print (p.name,p.pid) # Print (P.is_alive ()) #判断子进程是否运行 # P.join (t imeout=3) #将父进程与子进程的异步状态转换为同步状态, wait for the child process to complete the stepfather process and then run it; timeout value is too small to keep the async state # Print (777) #父进程等待子进程运行完再关闭 # DEF fn (i): # Print ( i**2) # # # if __name__ = = ' __main__ ': # lst=[]# for I in Range (2,6): # p=process (target=fn,args= (i)) # P.start () # P.join () # # Lst.append (P) # # [P.join () for p in lst]# print (666) #创建进程的另一种方式: Class My_process (Process): def-__init__ (self): Super (my_process, self). __init__ () def run: print ("This is a child process") if __name__ = = ' __main__ ': P=my_proce SS () Start=time.time () P.run () print (Time.time ()-start)
Process opening and its methods and properties