A Both invocation process
Called process similar thread
- Direct call to the custom of the objective function
from multiprocessing Import Process Import Time DEF myThreading (): the time.sleep ( 2 ) Print ( ' child thread running ' , time.ctime ()) DEF myThreading1 (): the time.sleep ( . 4 ) Print ( ' child thread 1 is run ' , time.ctime ()) IF the __name__ == ' __main__ ' : Q = Process (target = myThreading) Ql = Process (target = myThreading1) # q.daemon # Q1.daemon q.start () q1.start () # q.join () # q1.join () Print ( ' the main thread running ' , time.ctime ())
- inherited from the parent class and overrides its run function
from multiprocessing import Process import time class MyProcess(Process): def __init__(self): super(MyProcess, self).__init__() #self.name = name def run(self): time.sleep(1) 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 p in p_list: p.join() print('end')
Use different resources among multiple processes, different memory address natural
# From multiprocessing Import Process # n-# = 100 defined global variables should __name__ == '__main__' on it if in the windows system # DEF Work (): # Global n- # n-0 = # Print ( 'sub the process: ', n-) # # # direct process memory space is isolated # IF the __name__ ==' __main__ ': # P = process (target = Work) # p.start () # Print (' the main process: ', n)
To be continued. . . . . Thread queue, process pool