#multiprocess. Process Module #process module is a module for creating processes, with which you can complete the process creation # process ([Group[,target[,name[,args[,kwargs]]]), The object that is instantiated by this instantiation, # represents the task of a child process (not yet started) #注意 (parameter description): #1. You need to specify the parameter #2 by using the keyword parameter. args Specifies the positional parameter passed to the target function, which is a tuple form and must have a comma #3. The group parameter is used, and the value is never the None #4. Target represents the calling object, the task #5 that the child process will perform. Args represents the dictionary of the calling object, kwargs={"name_": "JSON", "Age": 19} #6. Name is the name of the child process #方法介绍: #1. P.start (): Starts the process and calls the P.run () method in the child process #2. P.run (): The method that runs at the start of the process, it is the function that calls the target specified, # We must implement this method #3 the class of our custom class. P.terminate (): Force terminate Process P, no cleanup operation #4. p.is_alive (): Returns True if still running 5.p.join (Timeout): The main process waits for P to terminate (note: The main process is in an equal state while P #是处于运行的状态) timeout is optional out of time, it needs to be emphasized that P.join can only join #主start开启的进程 , and cannot join the process that the run opens. #属性介绍: #1. P.daemon: The default value is False, and if set to True, represents p for the daemon that is running after the #2. P.pid: PID of the process Os.getpid () the PID #3 of the currently running process. P.ex Itcode: The process is none at run time, and if it is-N, the signal N ends #4. P.authkey: The authentication of the process, by default, is a randomly generated 32-bit character #字符串 by Os.urandom (). This key is used in order to involve the network connection Provides security for communication between the underlying #这类连接只有在具有相同的身份验证键时才能成功. #process模块创建进程和查看主进程的pid # from multiprocessing import process## import time,os## def fun C (name): # print ("Hello", name) # print ("I am a subprocess, my PID is%s, the parent process pid is%s"% (Os.getpid (), Os.getppid ())) # # if __name__ = = ' __ main__ ': # p = Process (target=func,args= ("Jack")) # tuple # P.start () # Start Progress # Time.sleep (1) # print ("The parent process has been executed , the PID of the parent process is%s, the father's parent process (PYCHARM) PID is%s "% (Os.getpid (), Os.getppid ())) #结果: # Hello Jack # I'm a subprocess, my PID is 4604, the PID of the parent process is 7728 # The parent process is executed, the PID of the parent process is 7728, the father's parent process (PYCHARM) PID is 4476 # # through inheritance create process # from multiprocessing Import process# class myprocess (Proc ESS): # def __init__ (self): # super (myprocess, self). __init__ () # # def run (self): # print ("Execute sub-process") # # F ROM multiprocessing import process# import os# class myprpcess (Process): # def __init__ (self,name): # super (MYPR Pcess, self). __init__ () # self.name = name# def run (self): # print (Os.getpid ()) # print ("%s chatting" %self.name) # if __name__ = = ' __main__ ': # p1 = myprpcess ("Jack") # P2 = myprpcess ("Tom") # # P1.start () #start会调用run方法 # P2.start () # # P1.join () # P2.join () # # print ("Main thread") # if __name__ = = ' __main__ ': # p = myprocess () # # P1.start () # refers to , the interpreter tells the operating system to help me open a process, ready state # P1.run () # Tell the operating system, now help me execute this child process Execution # Result: Execute subprocess # Join () Method: # from Multiprocessin G Import process# Import time# def func (name): # Time.sleep (2) # print ("Hesllo", name) # print ("Execute sub-process") # # if __nam e__ = = ' __main__ ': # p = Process (target=func,args= ("Jack")) # P.start () # p.join () # Print ("Execute main process") #结果: # HESL The LO jack# executes a subprocess # to perform the main process # join (): To allow the main process to wait for the process to finish executing. # phenomenon: The main process executes to this sentence, the main process is blocked, waiting for the child process child Process Execution # to open a normal child process, the parent process waits for the child process to end, the parent process is the program to end # How to make the relationship between the parent process and the child process become synchronous or asynchronous? #父进程执行join, it becomes synchronous, does not execute the join, and the parent and child processes are the asynchronous relationship #注意: #join must be placed after start () # Multiple processes cannot share memory # from multiprocessing import proces s## def func (i): # print ("I am%s"%i) # calls four times are independent of each other # #print (n) # cannot call n# if __name__ = = ' __main__ ': # n =100# addr = ["Henan", "Hubei", "Shandong", "Beijing"]# for i in addr:# p = Process (target=func,args= (i,)) # P.start () #结 Fruit: # I'm Henan # I'm Hubei # I'm a city in Shandong # I'm Beijing # multiple child processes (the execution order of a child process is not determined by the boot order) # from multiprocessing import process# import random# Import time# def func (i): # print ("I am%s"%i) # if __name__ = = ' __main__ ': # lst = []# addr = ["Henan", "Hubei", "Shandong", "Beijing" ]# for i in addr:# p = Process (target=func,args= (i,)) # P.start () # Lst.append (P) # time. Sleep (1) # [P.join () for p in lst]# print ("I choose%s"% (Random.choice (addr)) #结果: # I'm Henan # I'm Hubei # I'm a city of Shandong # I'm Beijing # I choose Beijing (randomly appearing) # more Process running concurrently, join method # from multiprocessing import process## def func (name): # print ("Hello", name) # # if __name__ = = ' __main__ ' : # LST = []# for I in range (5): # p = Process (target=func,args= ("Alex",)) # P.start () # LST.A Ppend (P) # p.join () # print ("Execute parent Process") #结果: # hello alex# hello alex# hello alex# hello alex# Hello alex# Execute parent Process # multi-process Same When running, the Join method is from MultipRocessing Import process# Import time## def func (name): # print ("Hello", name) # Time.sleep (1) # # if __name__ = = ' __ma in__ ': # LST = []# for I in range (5): # p = Process (target=func,args= ("Jack",)) # P.start () # Lst.append (P) # [P.join () for p in lst]# print ("Parent Process Execution") #结果: # hello jack# hello jack# hello jack# hello jack# Hello jack# Parent Process Execution
Python process creation process and methods