When using the win API to write programs, multi-process programs basically did not write, because multithreading has been able to meet most of the needs.
But there is a problem with Python's multithreading, in which threads in the process share a CPU core, similar to a single-threaded time shard. Before writing the time to remember that the problem is very difficult to deal with, and then did not engage.
1 fromMultiprocessingImportProcess2 ImportOS3 4 defRun_proc (name):5 Print 'Run child process is%s, PID =%d'%(name, Os.getpid ())6 7 if __name__=='__main__':8 Print 'Run Parent process pid is =%d'%os.getpid ()9Secondprocess = Process (target = run_proc, args = ('Test', ) )Ten Print 'Starting Second Process' One Secondprocess.start () A Print 'Child Process End'
With reference to a multi-process program written in the course of Liao Xuefeng, you can run 2 processes at the same time and then exit.
1 fromMultiprocessingImportProcess2 ImportOS3 4 defRun_proc (name):5 Print 'Run child process is%s, PID =%d'%(name, Os.getpid ())6val =0;7 while1:8Val + = 19 Ten if __name__=='__main__': One Print 'Run Parent process pid is =%d'%os.getpid () ASecondprocess = Process (target = run_proc, args = ('Test', ) ) - Print 'Starting Second Process' - Secondprocess.start () theval =0; - while1: -Val + = 1 - Print 'Child Process End'
Then add the dead Loop code to the two process files to see the CPU usage:
As you can see, the 2 threads running at the same time are sub-processor-capable and can take full advantage of the hardware performance.
Python Multi-process