Avoid python popen blocking __python

Source: Internet
Author: User

http://backend.blog.163.com/blog/static/2022941262014016710912/


Many development and operation personnel like to do some development or operation in Python. It is inevitable to invoke system commands.
There are two ways to invoke system commands, one is Os.system (CMD) and the other is subprocess. Popen (Cmd,stdout=what,stderr=what).
such as: Import OS os.system ("Echo Hi")
or import subprocess obj=subprocess. Popen ("Echo Hi")
But this usually does not meet the requirements. We generally need to parse the output after executing the command and parse out the information we want from the output.
The former can not meet the requirements, Os.system can not effectively output the result of cmd execution. You can only use the Subprocess method.
The usual practice is as follows:


Import subprocess Import Traceback
Try:cmd = "Ls-lh" obj = subprocess. Popen (cmd,stdout=subprocess. Pipe,stderr=subprocess.         pipe,shell=true) obj.wait () lines = Obj.stdout.readlines () If not lines or len (lines) = 0: line = Obj.stderr.readlines () print lines except Exception, E:print Traceback.format_exc ()

This will not be a problem in most cases. But there's a hidden danger here. is to find that one day the program hang Live. This may cause the entire application to be hung. When you have a lot of effort to locate the problem, you will find that the culprit is popen. And what's confusing is that you've tested this place for n times. How could hang live here?
The reason is that the pipe of subprocess is of a size. Before python2.6.11, the size of the pipe is the size of the file page (i386 is 4096), and 2.6.11 then becomes 65536. Therefore, when the output exceeds 65536, it can cause blocking. Because the pipe has been stuffed, no more data can be stuffed.
The workaround is to use the stream that you created instead of the pipe provided by subprocess. In this way, you can control the size of the stream. Not much said, directly on the code: Import subprocess Import traceback import tempfile try:cmd = "LS-LH" out_temp = tempfile. Spooledtemporaryfile (bufsize=10*1000) Fileno = Out_temp.fileno () obj = subprocess. Popen (cmd,stdout=fileno,stderr=fileno,shell=true) obj.wait () out_temp.seek (0) lines = out_temp.readlines ( ) print lines except Exception, E:print traceback.format_exc () finally:if Mp.close ()

Related Article

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.