Python Create process fork usage

Source: Internet
Author: User

This example describes the Python creation process fork usage. Share to everyone for your reference. The specific analysis is as follows:

?

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 #!coding=utf-8 import OS, traceback import time ' fork () system call is a system call that creates a subprocess on its own process under UNIX, one call, two returns, or a subprocess if the return is 0, if the return value > 0, is the parent process (the return value is the PID of the subprocess) ' ' Source = ten i = 0 try:print ' *********************** ' pid = os.fork () #这里会返回两次, so the following ellipsis will output 2 times the PRI NT ' ... ' if pid = = 0: #子进程 print ' This are child process ' Source = source-1 print ' child process source ', source Tim E.sleep (a) print ' child sleep do ' else: #父进程 print ' This is parent process ' print ' parent process source ', Source Ti Me.sleep (a) print ' Parent sleep done ' print source Except:traceback.print_exc ()

The output is as follows:

?

1 2 3 4 5 6 7 8 9 10 11 ... this is child process child process source is 9 ... this is parent process parent Proces s source is 9 parent-sleep-Done 10

I hope this article will help you with your Python programming.

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.