简体   繁体   English

Python子进程交互,为什么我的进程与Popen.communicate一起工作,而不是Popen.stdout.read()?

[英]Python subprocess interaction, why does my process work with Popen.communicate, but not Popen.stdout.read()?

I am trying to communicate with a command-line chat bot with Python using the subprocess module. 我正在尝试使用subprocess模块与Python的命令行聊天机器人进行通信。 (http://howie.sourceforge.net/ using the compiled win32 binary, I have my reasons!) (http://howie.sourceforge.net/使用编译的win32二进制文件,我有我的理由!)

This works: 这有效:

proc = Popen('Howie/howie.exe', stdout=PIPE,stderr=STDOUT,stdin=PIPE)
output = proc.communicate()

But Popen.communicate waits for the process to terminate (and sends it EOF?), I want to be able to interact with it. 但是Popen.communicate等待进程终止(并将其发送给EOF?),我希望能够与它进行交互。 The apparent solution for this was to read stdout / write stdin like so: 对此的明显解决方案是读取stdout / write stdin如下所示:

This doesn't work: 这不起作用:

proc = Popen('Howie/howie.exe', stdout=PIPE,stderr=STDOUT,stdin=PIPE)
while True: print proc.stdout.readline()

(Note that I am actually using more complex code based on http://code.activestate.com/recipes/440554/ but the issue is the same.) (请注意,我实际上使用的是基于http://code.activestate.com/recipes/440554/的更复杂的代码,但问题是相同的。)

The problem is, the second approach works perfectly for communicating to cmd, but when I run the chatbot, nothing. 问题是,第二种方法非常适合与cmd进行通信,但是当我运行聊天机器人时,没有任何内容。 So my question is, how is this different in capturing output to using Popen.communicate()? 所以我的问题是, 在使用Popen.communicate()捕获输出时有什么不同?

ie I can use the second approach to use the command line as per normal, until I run the chatbot, at which point I stop receiving output. 即我可以使用第二种方法按照正常情况使用命令行,直到我运行chatbot,此时我停止接收输出。 Using the first approach correctly displays the first few lines of output from the bot, but leaves me unable to interact with it. 使用第一种方法正确显示机器人的前几行输出,但让我无法与它进行交互。

One major difference between the two is that communicate() closes stdin after sending the data. 两者之间的一个主要区别是communication()在发送数据后关闭stdin。 I don't know about your particular case, but in many cases this means that if a process is awaiting the end of the user input, he will get it when communicate() is used, and will never get it when the code blocks on read() or readline(). 我不知道你的具体情况,但在很多情况下,这意味着如果一个进程正在等待用户输入的结束,他将在使用communic()时获取它,并且当代码阻塞时它将永远不会得到它read()或readline()。

Try adding Popen.stdin.close() first and see if it affects your case. 首先尝试添加Popen.stdin.close()并查看它是否会影响您的情况。

If you want to interact with the program after sending the EOF, rather than using Popen.stdin.close() , you can manually send the command-line End Of File character, which has the same effect but leaves stdin open. 如果要在发送EOF后与程序进行交互,而不是使用Popen.stdin.close() ,则可以手动发送命令行End Of File字符,该字符具有相同的效果但会打开stdin。

In Python this character's escape sequence is '\\x1a' . 在Python中,这个角色的转义序列是'\\x1a'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python子进程Popen.communicate()相当于Popen.stdout.read()? - Python subprocess Popen.communicate() equivalent to Popen.stdout.read()? python 3 Popen(半)交互式来回通信与进程Popen.communicate()vs Popen.stdin / Popen.stdout - python 3 Popen (semi-)interactive back-and-forth communication with a process, Popen.communicate() vs Popen.stdin/Popen.stdout subprocess popen.communicate()与stdin.write()和stdout.read() - subprocess popen.communicate() vs. stdin.write() and stdout.read() 有没有一种方法可以使用python子进程模块的Popen.communicate()而无需等待进程退出? - Is there a way to use the python subprocess module's Popen.communicate() without waiting for the process to exit? Python 子进程模块中 Popen.communicate() 的非阻塞版本 - Non-blocking version of Popen.communicate() in Python Subprocess module Python-使用Popen.communicate()不能按预期捕获系统调用的stdout和stderr - Python - Using Popen.communicate() does not capture the stdout and stderr of a system call as expected 如何使用子流程Popen.communicate()方法? - How to use the subprocess Popen.communicate() method? 了解Popen.communicate - Understanding Popen.communicate Python Popen.communicate()内存限制的替代方案? - Alternatives to Python Popen.communicate() memory limitations? 具有多个stdin写入的python popen.communicate() - python popen.communicate() with multiple stdin writes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM