From: Marcelo T. <re...@bu...> - 2009-06-04 06:12:33
|
New submission from Marcelo Taube <mai...@gm...>: I am using the popen2.popen2 function to open a child and get a pipe to him. Unfortunatelly when i print to the pipe the child does not get any output. Unfortunatelly i dont have internet connection in the computer i work with so i am not able to submit exactly the same program that caused the error, however i will post here some similar code, as similar as my memory allows. <Code> 1: child_stdout, chld_stdin = popen("/usr/bin/tee") 2: child_stdin.write("This is a line\n") 3: child_stdin.flush() 4: out = child_stdout.readline() 5: print "from child:", out </code> The code above should print “from child: This is a line” because tee prints back its line it receives. The code works as it should in cpython but in jython it gets stucked in line 4 when trying to read. The problem in jython is that the pipe is not flushed to ‘tee’, ‘tee’ gets not input and consequently it does not print back any value. Thus trying to read from child_stdout blocks the program to the end of the days. I have been doing some debugging and the problem seems to happen also with subprocess.Popen Since I have not enough knowledge of jython and the Popen implementation I cannot tell exactly what is the problem. In spite of that, I still could understand some of the code and I see that the pipe is implemented as a python file wrapping an object of type org.python.core.io.StreamIO wrapping the output stream returned by ‘java.lang.Process.getOutputStream(..)’ . I copied-pasted that code and to a function of mine and tried to play with it. I arrived at the conclution that there are two places to flush. The jython file handler has a flush function and the java output stream has another flush function that never gets flushed, that means that even when the jython file is not buffered, output is never printed. This pseudo-code will do the work that the previous pseudo-code should have done: <code> procBuild = java.lang.ProcessBuilder(["/usr/bin/tee"]) proc = procBuild.start() proc_out = proc.getOutputStream() child_stdout = org.python.core.io.StreamIO (proc_out, True) #copied True from the Popen code child_stdin = org.python.core.io.StreamIO (proc.getInputStream() , True) child_stdin.write("This is a line\n") child_stdin.flush() proc_out.flush() #NOTICE THIS LINE!!! out = child_stdout.readline() print "from child:", out </code> ---------- components: Core messages: 4794 nosy: marcelotaube severity: normal status: open title: PIpes (popen2) do not flush their buffer versions: 25b3 _______________________________________ Jython tracker <re...@bu...> <http://bugs.jython.org/issue1367> _______________________________________ |