This is basically a short summary version of this article, which I got my answer from.
- If you use redirect stdout of a children process through PIPE, it will hang when the size of output is bigger than 64KB. (And it does not give you any meaningful error message. You might see some deadlock deep into some file handling functinos in ntdll or so)
- My advice is NEVER use PIPE with popen. Even if your current subprocess never outputs more than 64K, other programmers can change this shallow assumption sometime later. (This was my case)
- Instead, always use a tempfile. (see the code example below)
Code examples:
- Not-so-good code:
p = Popen(cmd, stdout=PIPE)
...
out_lines = p.stdout.readlines()
- Better code:
temp_file = tempfile.TemporaryFile()
d = temp_file.fileno()
p = Popen(cmd, stdout = d)
...
out_lines = d.readlines()
p.s.1 You don't have to close temp_file. It'll be closed when garbage collector collects this. But still donig so would be a good practice.
p.s.2 This was done in Python 2.5.1. The reason why I'm saying this is because I just heard Python 3.0 is not backward-compatible.


2 Comments:
I cannot read the file d, python says it's impossible to read an int object.
can you post your code here?
Post a Comment