Prev: The inverse of .join
Next: how to get bit info
From: Stephen Hansen on 17 Jun 2010 17:36 On 6/17/10 2:09 PM, Laurent Verweijen wrote: > 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) > '' Yes, that's how it signals the same situation. The point is: your subprocess isn't outputting anything. You sure its not crashing out, for instance? -- 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:37 Op donderdag 17-06-2010 om 23:09 uur [tijdzone +0200], schreef Laurent Verweijen: > 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) > '' > > I also tried running the module as a program: from asynchronous import * p = Popen(["python", "increment.py"], stdin=PIPE, stdout=PIPE) for n in [5, 7, 10, 4]: send_all(str(p), n) print(recv_some(p)) It gives: prompt:~$ python subchronous_test.py Traceback (most recent call last): File "subchronous_test.py", line 5, in <module> send_all(str(p), n) File "/home/Somelauw/asynchronous.py", line 145, in send_all while len(data): TypeError: object of type 'int' has no len() prompt:~$ Traceback (most recent call last): File "increment.py", line 4, in <module> n = int(raw_input(n)) + 1 EOFError: EOF when reading a line close failed in file object destructor: Error in sys.excepthook: Original exception was: By the way: synchronous is the name I gave to your module
From: Laurent Verweijen on 17 Jun 2010 17:40 Op donderdag 17-06-2010 om 14:36 uur [tijdzone -0700], schreef Stephen Hansen: > On 6/17/10 2:09 PM, Laurent Verweijen wrote: > > 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) > > '' > > Yes, that's how it signals the same situation. The point is: your > subprocess isn't outputting anything. You sure its not crashing out, for > instance? > No, since it responds to the keyboard: prompt:~$ python increment.py 05 64 53 44 55 66 75 64 53 44 55 66 7 All output is correct.
From: Stephen Hansen on 17 Jun 2010 17:48 On 6/17/10 2:40 PM, Laurent Verweijen wrote: > Op donderdag 17-06-2010 om 14:36 uur [tijdzone -0700], schreef Stephen > Hansen: >> On 6/17/10 2:09 PM, Laurent Verweijen wrote: >>> 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) >>> '' >> >> Yes, that's how it signals the same situation. The point is: your >> subprocess isn't outputting anything. You sure its not crashing out, for >> instance? >> > > No, since it responds to the keyboard: That doesn't really prove the point. There's all kinds of things that can go wrong when you switch how you run a program. Wrap your increment.py in like: import sys import traceback try: ... except: print >>sys.stderr, traceback.format_exc() Then add the arg in your Popen, stderr=sys.stderr And see if any exception is thrown. The original error you got, and the empty string from the recipe, both mean interpret.py is not returning any output. Why? Maybe its erroring out-- the subprocess context is different then the context of running a program from the keyboard. Or maybe you're not sending: In your other thread you include an actual traceback: Traceback (most recent call last): File "subchronous_test.py", line 5, in <module> send_all(str(p), n) File "/home/Somelauw/asynchronous.py", line 145, in send_all while len(data): TypeError: object of type 'int' has no len() The first argumetn to send_all should be the actual Popen subclass. The second should be a string to send. I think that line really is intended to be: send_all(p, str(n)) # assuming 'n' is say, the number 5. -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
From: Laurent Verweijen on 17 Jun 2010 18:06
Op donderdag 17-06-2010 om 14:48 uur [tijdzone -0700], schreef Stephen Hansen: > On 6/17/10 2:40 PM, Laurent Verweijen wrote: > > Op donderdag 17-06-2010 om 14:36 uur [tijdzone -0700], schreef Stephen > > Hansen: > >> On 6/17/10 2:09 PM, Laurent Verweijen wrote: > >>> 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) > >>> '' > >> > >> Yes, that's how it signals the same situation. The point is: your > >> subprocess isn't outputting anything. You sure its not crashing out, for > >> instance? > >> > > > > No, since it responds to the keyboard: > > That doesn't really prove the point. There's all kinds of things that > can go wrong when you switch how you run a program. > > Wrap your increment.py in like: > > import sys > import traceback > > try: > ... > except: > print >>sys.stderr, traceback.format_exc() > > Then add the arg in your Popen, stderr=sys.stderr > > And see if any exception is thrown. > > The original error you got, and the empty string from the recipe, both > mean interpret.py is not returning any output. Why? Maybe its erroring > out-- the subprocess context is different then the context of running a > program from the keyboard. Or maybe you're not sending: I did exactly what you said, here is the result: prompt:~$ python subchronous_test.py Traceback (most recent call last): File "increment.py", line 7, in <module> n = int(raw_input(str(n))) + 1 EOFError: EOF when reading a line prompt$ close failed in file object destructor: Error in sys.excepthook: Original exception was: > > In your other thread you include an actual traceback: > > Traceback (most recent call last): > File "subchronous_test.py", line 5, in <module> > send_all(str(p), n) > File "/home/Somelauw/asynchronous.py", line 145, in send_all > while len(data): > TypeError: object of type 'int' has no len() > > The first argumetn to send_all should be the actual Popen subclass. The > second should be a string to send. I think that line really is intended > to be: > > send_all(p, str(n)) # assuming 'n' is say, the number 5. > You are right, I swapped the parameters, but even if I correct it, it still gives the second error. |