Python creation 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:

#!coding=utf-8import OS, Tracebackimport time ' fork () system call is a system call that creates a child process under UNIX under its own process, 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 child process) ' Source = 10i = 0try:  print ' *********************** '  pid = os.fork ()  #这里会返回两次, So the ellipsis below will output 2 times  print ' ... '  if pid = = 0: #子进程 print "This is a child    process"    Source = source-1    Print ' Child process source is ', source    time.sleep (+)    print ' Child sleep done '  else:  #父进程    print " This is the parent process "    print ' parent process source is ', source    Time.sleep (Ten)    print ' Parent sleep done ' C16/>print sourceexcept:  traceback.print_exc ()

The output is as follows:

... this is a child processchild process source was 9......this is the parent processparent process sour Ce is 10child sleep done9parent sleep Done10

Hopefully this article will help you with 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.