Skip to content Skip to sidebar Skip to footer

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. (http://howie.sourceforge.net/ using the compiled win32 binary, I have my reasons!)

Solution 1:

One major difference between the two is that communicate() closes stdin after sending the data. 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().

Try adding Popen.stdin.close() first and see if it affects your case.

Solution 2:

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.

In Python this character's escape sequence is '\x1a'.

Post a Comment for "Python Subprocess Interaction, Why Does My Process Work With Popen.communicate, But Not Popen.stdout.read()?"