Python popen stuck to the dead.

Source: Internet
Author: User

Program often die, positioning for a half-day only to locate the reason, the original is popen caused by the card dead;

The procedure is as follows:

s = subprocess. Popen ([*,*,*], stdout=subprocess. PIPE)

ret = S.stdout.read ()

return ret

The official documentation is interpreted as:

This would deadlock when using and/or and the child stdout=PIPE stderr=PIPE process generates enough output to a pipe such that it bloc KS waiting for the OS pipe buffer to accept more data. Use to communicate() avoid.

The reason is that Popen. wait after using () direct read PIPE.stdout.read (), the cache may be full, causing the card to die at this time.

Workaround: Use Communicate ()

For example:

s = subprocess. Popen ([*,*,*], stdout=subprocess. PIPE)

Stdoutdata, Stderrdata = S.communicate ()

Return Stdoutdata

In addition, finally, when calling Popen, add the parameter close_fds=true, see the official documentation Note:

Popen2 closes all file descriptors by default, and you had to specify close_fds=true with Popen

Later use Popen or be careful, this inside pit many.

Python popen stuck to the dead.

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.