Prev: Python 2.4.4 Tkinter GUI Example for Modeless Dialog with Menu,Edit, List, Buttons, File Save etc.
Next: List of lists surprising behaviour
From: Laurent Verweijen on 17 Jun 2010 05:22 I have a file called increment.py as follows: #!/usr/bin/python n = 0 while True: n = int(raw_input(n)) + 1 This is easy to understand, but I want to pipe it's input/output by another python program. (I will show what I am doing on the console to test it) >>> from subprocess import * >>> p = Popen(["python", "increment.py"], stdin=PIPE, stdout=PIPE) >>> p.communicate("5") Traceback (most recent call last): File "increment.py", line 4, in <module> n = int(raw_input(n)) + 1 EOFError: EOF when reading a line ('06', None) >>> p.communicate("7") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/subprocess.py", line 701, in communicate return self._communicate(input) File "/usr/lib/python2.6/subprocess.py", line 1184, in _communicate self.stdin.flush() ValueError: I/O operation on closed file The problem is that I want to use communicate multiple times before closing the file. How could I achieve this
From: Nobody on 18 Jun 2010 20:45
On Thu, 17 Jun 2010 11:22:37 +0200, Laurent Verweijen wrote: > This is easy to understand, but I want to pipe it's input/output > by another python program. (I will show what I am doing on the > console to test it) > > >>> from subprocess import * > >>> p = Popen(["python", "increment.py"], stdin=PIPE, stdout=PIPE) > >>> p.communicate("5") > Traceback (most recent call last): > File "increment.py", line 4, in <module> > n = int(raw_input(n)) + 1 > EOFError: EOF when reading a line > The problem is that I want to use communicate multiple times > before closing the file. > > How could I achieve this Don't use communicate(). Use p.stdin.write() to send input, use p.stdin.close() then p.wait() when you're finished. If you also want to read the child process' stdout, either: 1. ensure that the child *never* writes more than a buffer's worth of data at a time (the exact value is implementation-dependent, but up to 4K should be fine for any modern Unix; I don't know about Windows), 2. use a separate thread for reading stdout, 3. use non-blocking I/O (use the fcntl module for Unix, you need PyWin32 for Windows), or 4. have the process write to a file instead of a pipe. See the implementation of subprocess.Popen.communicate for clues. |