subprocess.Popen.communciate()与stdin一起损坏的管道
使用subprocess.Popen.communicate()时出现一个奇怪的问题。 为了背景,我想从我的python脚本执行一个应用程序。 当我从命令行运行程序时,我这样做(UNIX):
$ echo "input text" | /path/to/myapp
从我的脚本中,我也想将输入输入到应用程序中。 所以,我尝试了以下。 但是当我尝试使用communic()发送输入时,出现“断开的管道”错误:
>>> cmd = ['/path/to/myapp'] >>> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) >>> out,err = p.communicate('input text') Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.5/subprocess.py", line 670, in communicate return self._communicate(input) File "/usr/lib/python2.5/subprocess.py", line 1223, in _communicate bytes_written = self._write_no_intr(self.stdin.fileno(), buffer(input, input_offset, 512)) File "/usr/lib/python2.5/subprocess.py", line 1003, in _write_no_intr return os.write(fd, s) OSError: [Errno 32] Broken pipe
为了让事情变得更奇怪,如果我忽略输入数据,我不会有任何错误。 但是,这不是一个很好的解决方法,因为应用程序需要输入才能工作。
>>> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) >>> out,err = p.communicate() >>> print out [error from myapp regarding lack of input]
任何想法我失踪?
你的观察表明,myapp在没有阅读(全部)输入的情况下终止。 不知道myapp有什么,这很难确认,但考虑例如
$ echo 'hello world' | tr 'l' 'L'
heLLo worLd
现在...:
>>> cmd = ['/usr/bin/tr']
>>> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
>>> out,err = p.communicate('hello world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/subprocess.py", line 668, in communicate
return self._communicate(input)
File "/usr/lib/python2.5/subprocess.py", line 1218, in _communicate
bytes_written = self._write_no_intr(self.stdin.fileno(), buffer(input, input_offset, 512))
File "/usr/lib/python2.5/subprocess.py", line 997, in _write_no_intr
return os.write(fd, s)
OSError: [Errno 32] Broken pipe
因为...:
>>> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
>>> /usr/bin/tr: missing operand
Try `/usr/bin/tr --help' for more information.
如果我们修复了这个错误:
>>> cmd = ['/usr/bin/tr', 'l', 'L']
>>> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
>>> out,err = p.communicate('hello world')>>> print out
heLLo worLd
>>> print err
None
它修复了一切。 如果您省略stderr重定向会发生什么情况 - 您是否会看到myapp中的任何错误消息...?
链接地址: http://www.djcxy.com/p/77105.html上一篇: Broken Pipe from subprocess.Popen.communciate() with stdin