Prev: The inverse of .join
Next: how to get bit info
From: Laurent Verweijen on 17 Jun 2010 15:13 I have a program called increment.py as follows: #!/usr/bin/python n = 0 while True: n = int(raw_input(n)) + 1 This is probably very easy to understand, but I want to run this program from another python program. Below is an attempt >>> 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 How do I make sure the inputstream stays open after the first call to communicate?
From: Stephen Hansen on 17 Jun 2010 16:01 On 6/17/10 12:13 PM, Laurent Verweijen wrote: > How do I make sure the inputstream stays open after the first call to > communicate? This was just asked a few days ago in different words-- check out the thread, a couple solutions are offered. In short, you need to make stdin/stdout non-blocking: http://groups.google.com/group/comp.lang.python/browse_thread/thread/066de1c0fd38642f# -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
From: Laurent Verweijen on 17 Jun 2010 16:42 Op donderdag 17-06-2010 om 13:01 uur [tijdzone -0700], schreef Stephen Hansen: > On 6/17/10 12:13 PM, Laurent Verweijen wrote: > > How do I make sure the inputstream stays open after the first call to > > communicate? > > This was just asked a few days ago in different words-- check out the > thread, a couple solutions are offered. In short, you need to make > stdin/stdout non-blocking: > > http://groups.google.com/group/comp.lang.python/browse_thread/thread/066de1c0fd38642f# > I tried putting what Ian Kelly said in my code, by it doesn't work for me. Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> import fcntl >>> import subprocess >>> process = subprocess.Popen(["python", "increment.py"], stdin = subprocess.PIPE, stdout = subprocess.PIPE) >>> flags = fcntl.fcntl(process.stdout, fcntl.F_GETFL) >>> fcntl.fcntl(process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) 0 >>> process.stdin.write("5\n") >>> process.stdout.read() Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 11] Resource temporarily unavailable
From: Stephen Hansen on 17 Jun 2010 16:48 On 6/17/10 1:42 PM, Laurent Verweijen wrote: > I tried putting what Ian Kelly said in my code, by it doesn't work for > me. > > Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) > [GCC 4.4.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import os >>>> import fcntl >>>> import subprocess >>>> process = subprocess.Popen(["python", "increment.py"], stdin = > subprocess.PIPE, stdout = subprocess.PIPE) >>>> flags = fcntl.fcntl(process.stdout, fcntl.F_GETFL) >>>> fcntl.fcntl(process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) > 0 >>>> process.stdin.write("5\n") >>>> process.stdout.read() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > IOError: [Errno 11] Resource temporarily unavailable I *believe* that error in response to "read()" is something you should catch: its EAGAIN. Meaning, for it to perform that operation, it would have to block, but you've set it to not block. Thus, your subprocess hasn't written anything new out yet by the time you call that. You have to try/except looking for that and catch it. That's why I preferred the recipe I linked to in that thread: it uses select to only read when there's something -to- actually read. -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
From: Laurent Verweijen on 17 Jun 2010 17:09
Op donderdag 17-06-2010 om 13:48 uur [tijdzone -0700], schreef Stephen Hansen: > On 6/17/10 1:42 PM, Laurent Verweijen wrote: > > I tried putting what Ian Kelly said in my code, by it doesn't work for > > me. > > > > Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) > > [GCC 4.4.3] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > >>>> import os > >>>> import fcntl > >>>> import subprocess > >>>> process = subprocess.Popen(["python", "increment.py"], stdin = > > subprocess.PIPE, stdout = subprocess.PIPE) > >>>> flags = fcntl.fcntl(process.stdout, fcntl.F_GETFL) > >>>> fcntl.fcntl(process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) > > 0 > >>>> process.stdin.write("5\n") > >>>> process.stdout.read() > > Traceback (most recent call last): > > File "<stdin>", line 1, in <module> > > IOError: [Errno 11] Resource temporarily unavailable > > I *believe* that error in response to "read()" is something you should > catch: its EAGAIN. Meaning, for it to perform that operation, it would > have to block, but you've set it to not block. > > Thus, your subprocess hasn't written anything new out yet by the time > you call that. You have to try/except looking for that and catch it. > > That's why I preferred the recipe I linked to in that thread: it uses > select to only read when there's something -to- actually read. > It just gives me an empty string. Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from asynchronous import * >>> p = Popen(["python", "increment.py"], stdin=PIPE, stdout=PIPE) >>> send_all(p, "5\n") >>> recv_some(p) '' >>> send_all(p, "6\n") >>> recv_some(p) '' |