0

I have this program where I'm trying to launch a separate console based application whose output I will monitor and provide appropriate input when asked. But it's getting stuck at

c = proc.stdout.read(1)

Whereas it works perfectly when ran from command line directly

Code snippet:

      cmd = "/usr/bin/App.out"
      proc = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
      c = proc.stdout.read(1)
      print c

I have tried plenty of options such as:

  • Not using 1 byte buffer with read()
  • using readline()
  • using TemporaryFile instead of PIPE
  • checked root access
  • Used communicate() to check if any stderr is there for 'proc', but there is no error
  • Consider using `communicate()` instead http://stackoverflow.com/q/2715847/4279 – Chen A. Apr 16 '18 at 12:00
  • I have already mentioned in the question that communicate() is also not working. – Deepak Nayak Apr 16 '18 at 12:04
  • If you are not passing data to the standard input of the separate process, don't pass `PIPE` to stdin, but `None` or leave unspecified. Also, is the other process waiting for input? You are reading (and waiting for) its output, but if it is waiting for input before writing anything to stdout, it will deadlock... You can also try to pass `bufsize=0` to use unbuffered IO. – Marco Pantaleoni Apr 16 '18 at 12:05
  • I do need to pass data to the standard input when I receive a standard prompt from the process e.g. proc.stdin.write('1\n'). The other process do need to wait for an input but only after printing some lines (e.g. menu options). So I'm waiting for the other process to give the prompt after which I can write something. Also tried bufsize=0 but still not working. – Deepak Nayak Apr 16 '18 at 12:08
  • does the console application use something like curses to perform its I/O? – Marco Pantaleoni Apr 16 '18 at 12:18
  • No there are no curses. It's a simple c++ based application which just asks user for it's selection and performs appropriate tasks based on the input given. – Deepak Nayak Apr 16 '18 at 12:32
  • any chance to see a gist of that code? (or a stripped down version) – Marco Pantaleoni Apr 16 '18 at 12:39
  • Maybe the called program needs to flush its output to make it appear to your python program. – Arndt Jonasson Apr 16 '18 at 13:00
  • @MarcoPantaleoni That app is too big. I'll see what I can do to have a stripped down version. – Deepak Nayak Apr 17 '18 at 03:29
  • @ArndtJonasson I'll check on it. – Deepak Nayak Apr 17 '18 at 03:30

0 Answers0