0

I have an interactive Windows console application that I communicate through subprocess module in my program. So far, the console commands(of the application) that I called from my script did not made any problems because they gave instant results(pipe outputs).

However there are other functionalities I need to use that keeps printing output to the console until it recieves "stop" command. I need to read those outputs, but my application is a GUI and I cannot let any hanging occur, and I don't want to get all the output after stop command is issued, I need to get it realtime, when it is served. I made some research and it appears this is a difficult problem to overcome(especially in Windows), but there are no recent questions about this topic.

I did find this, Non-blocking read on a subprocess.PIPE in python, looks like what I am looking for.

But the answer seems really low-level for a Python solution and it is old. Are there any better-newer solutions for this problem ?

Thanks in advance.

Community
  • 1
  • 1
Rockybilly
  • 2,938
  • 1
  • 13
  • 38
  • `pexpect` is a library that could be used for nonblocking reading spawned process's output, but it's not that easy for Windows http://pexpect.readthedocs.io/en/stable/overview.html#windows – Andrew Che May 10 '17 at 05:31
  • @AndrewCherevatkin I've read about `pexpect`, however the Windows part and having absent methods gave me worry, because I do not know which functionalities are not usable. – Rockybilly May 10 '17 at 05:34
  • Not for Python 2.7. In Python 3 you can use [`asyncio`](https://docs.python.org/3/library/asyncio-subprocess.html) to avoid blocking. However, if the problem is that the program itself buffers output when its stdout is a pipe, then there's no general solution for Windows. `pexpect` for Windows uses pipes and offers no solution to this problem. – Eryk Sun May 10 '17 at 07:15
  • Maybe you could redirect the program's output to a file and read and clean it with some interval? Makes a risk of losing some output though. – Andrew Che May 10 '17 at 09:16

0 Answers0