与一个进程进行多次通信而不破坏管道?
这不是我第一次遇到这个问题,而且它确实困扰着我。 每当我使用 Python subprocess
模块打开管道时,我只能与它通信
一次,正如文档指定的那样:从 stdout 和 stderr 读取数据,直到结束-of-file is returned
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
是否允许多次通信?
It's not the first time I'm having this problem, and it's really bugging me.
Whenever I open a pipe using the Python subprocess
module, I can only communicate
with it once, as the documentation specifies: 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]
The problem here is that the second time, Python isn't happy. Indeed, he decided to close the file after the first communicate:
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
Are multiple communications allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你误解了沟通...
http://docs.python.org /library/subprocess.html#subprocess.Popen.communicate
communications 发送一个字符串到另一个进程,然后等待它完成...(就像你说的等待 EOF 监听 stdout 和 stderror)
你应该做的是:(
如果你需要获取 stdout 或 stderr,你可以使用 proc.stdout 或 proc.stderr)
I think you misunderstand communicate...
http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate
communicate sends a string to the other process and then waits on it to finish... (Like you said waits for the EOF listening to the stdout & stderror)
What you should do instead is:
(and if you need to get the stdout or stderr you'd use proc.stdout or proc.stderr)
我以前遇到过这个问题,据我所知,你不能用
subprocess
来做到这一点(我同意,如果这是真的,那是非常违反直觉的)。我最终使用了 pexpect(可从 PyPI 获得)。I've had this problem before, and as far as I could ever figure, you couldn't do this with
subprocess
(which, I agree, is very counterintuitive if true). I ended up usingpexpect
(obtainable from PyPI).您只需调用一次
communicate()
即可完成此操作:这里的关键点是您只向
stdin
写入一次,并且\n
作为 EOL。你的psql子进程从
stdin
读取直到\n
,然后在完成第一个查询后,它再次转到stdin
,此时只有第二个查询字符串留在缓冲区中。You can do this simply with single call of
communicate()
:The key-point here is that you only write once to
stdin
, and\n
serve as EOL.your psql subprocess reads from
stdin
until\n
, then after it finishes the first query, it goes tostdin
again, by which time only the second query string is left in the buffer.您可以使用:
You can use: