【发布时间】:2011-03-05 03:36:54
【问题描述】:
这不是我第一次遇到这个问题,而且真的很困扰我。
每当我使用 Python subprocess 模块打开管道时,我只能使用它一次 communicate,如文档所述:Read data from stdout and stderr, until end-of-file is reached
proc = sub.Popen("psql -h darwin -d main_db".split(),stdin=sub.PIPE,stdout=sub.PIPE)
print proc.communicate("select a,b,result from experiment_1412;\n")[0]
print proc.communicate("select theta,zeta,result from experiment_2099\n")[0]
这里的问题是第二次,Python 不高兴。的确,他决定在第一次沟通后关闭文件:
Traceback (most recent call last):
File "a.py", line 30, in <module>
print proc.communicate("select theta,zeta,result from experiment_2099\n")[0]
File "/usr/lib64/python2.5/subprocess.py", line 667, in communicate
return self._communicate(input)
File "/usr/lib64/python2.5/subprocess.py", line 1124, in _communicate
self.stdin.flush()
ValueError: I/O operation on closed file
是否允许多次通信?
【问题讨论】:
-
对于 psql,有很多现有的 Python 包装器:wiki.python.org/moin/PostgreSQL
标签: python pipe subprocess