042 multi-process,
Content: Process Creation (two methods)
#############################
Method 1: Create a thread object and set the parameters of the method and method for executing the process.
from multiprocessing import Processimport timedef f(name): time.sleep(0.2) print('hello',name,time.ctime())if __name__ == '__main__': p_list = [] for i in range(3): p = Process(target=f,args=('xxx',)) p_list.append(p) p.start() for i in p_list: i.join() print('end')
#############################
Method 2:
1. Create a class to inherit the Process class
2. Override the run method in the class.
3. Create an object and execute the start method to enable the thread.
From multiprocessing import Processimport timeclass MyProcess (Process): def _ init _ (self): super (MyProcess, self ). _ init _ () def run (self): time. sleep (0.2) print ('hello', self. name, time. ctime () if _ name _ = '_ main _': p_list = [] for I in range (3): p = MyProcess () p. start () p_list.append (p) for t in p_list: t. join () print ('end') # running result: # hello MyProcess-1 Sun Feb 4 09:19:07 2018 # hello MyProcess-2 Sun Feb 4 09:19:07 2018 # hello MyProcess-3 Sun Feb 4 09:19:07 2018 # end
Self. name is the default start name, which can be changed by yourself.