1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
import multiprocessing import os import time
class MainProcess: def __init__(self, main_process_time, child_process_time): self.main_process_time = main_process_time self.child_process_time = child_process_time
def excutor(self): print('main process begin, pid={0}, ppid={1}'.format(os.getpid(), os.getppid())) p = ChildProcess(self.child_process_time) p.start() p.join(3) for i in range(self.main_process_time): print('main process, pid={0}, ppid={1}, times={2}'.format(os.getpid(), os.getppid(), i)) time.sleep(1)
class ChildProcess(multiprocessing.Process): def __init__(self, process_time): multiprocessing.Process.__init__(self) self.process_time = process_time
def run(self): print('child process begin, pid={0}, ppid={1}'.format(os.getpid(), os.getppid())) for i in range(self.process_time): print('child process pid={0}, ppid={1}, times={2}'.format(os.getpid(), os.getppid(), i)) time.sleep(1) print('child process end, pid={0}, ppid={1}'.format(os.getpid(), os.getppid()))
if __name__ == '__main__': main_process_time = 15 child_process_time = 10 action = MainProcess(main_process_time, child_process_time) action.excutor()
|