Python Combat child Process __python

Source: Internet
Author: User

A recent gadget need to use the Python word process, look at the Python api,subprocess module can be implemented, and quite simple

To implement a simple self process, you need only

Import sys, OS, subprocess p = subprocess. Popen (["LS", "L"], Shell=false, Stdin=none, Stdout=none, stderr=none) print p.pid print P.returncode

At this point, the PID will be the ID number of the subprocess, and ReturnCode will be none if the child process is rolled out normally.

You can also use the following code to create a child process

Import sys, OS, subprocess retcode=subprocess.call (["LS", "L"], Shell=false, Stdin=none, Stdout=none, Stderr=none) Print Retcode

At this point, if the subprocess is rolled out normally, Retcode will be 0.

In addition, you can set the stdout and stderr in the method parameters to subprocess If you do not want to output the print information for the created child process. PIPE.

Here is a small example of creating a subprocess P1 and then creating a subprocess P2 to end P1 execution.

Import sys, OS, subprocess p1 = subprocess. Popen (["Sleep", "M"], Shell=false, Stdin=none, Stdout=none, stderr=none) print p1.pid print p1.returncode P2 = subprocess . Popen (["Kill", str (P1.PID)], Shell=false, Stdin=none, Stdout=none, stderr=none) print p2.pid print P2.returncode

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.