2013-12-13 3 views
3

는 다음 코드를파이썬 2.7 및 3.3 서브 프로세스 모듈 차이

from __future__ import print_function 
import subprocess 

p = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 
p.kill() 
p.wait() 
print(p.stdin.write(b'some')) 

을하고 난 파이썬 2.7 파이썬 3.3로 달렸다. 파이썬 2.7에서는 stdin에 쓰려고 할 때 예외가 발생합니다. python 3.3에서는이 코드가 인쇄됩니다. 4. python 2.7 하위 프로세스 모듈과 python 3.3의 차이점은 무엇입니까?

+0

문서 도구 [2.7] (http://docs.python.org/2.7/library/subprocess.html#popen-objects) 및 [3.3] (HTTP : //docs.python .org/3.3/library/subprocess.html # popen-objects)'.stdin.write'보다는'communicate'를 사용하도록 경고합니다 - 차이가 있습니까? – jonrsharpe

+1

Python 2.7에서 어떤 표현식이 던져 졌는지 알면 좋을 것입니다 : – jbaiter

+0

예외는'IOError : [Errno 32] Broken pipe'입니다. 'communicate'를 사용할 때 예외는 발생하지 않지만'stdin.write'를 사용해야합니다. – sinitsynsv

답변

4

차이는 bufsize의 기본값입니다. Python 2.7에서 0 (버퍼되지 않음)이므로 writeEPIPE 오류를 발생시킵니다. 파이프는 Python 3.2 이상에서 완벽하게 버퍼링됩니다. 즉, 버퍼를 플러시 할 때까지 오류가 감지되지 않습니다. subprocess' docs에서 : 모두를위한

Changed in version 3.3.1: bufsize now defaults to -1 to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to 0 which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.

관련 문제