Python Multi-process

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.