473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to flush child_stdin

I'm opening up a subprocess like this where slave.py is a text based
app that receives commands and responds with output:

r, w, e = popen2.popen3('python slave.py')

I need to send slave.py a command and see the output,
so I'll do something like:

w.write("command here")
then i'll try this:
w.flush()

A separate thread is reading from r to retrieve output of slave.py.

The problem is that slave.py doesn't seem to receive commands unless I
also do:
w.close()

But I don't want to do this because I'll want to send more commands.

Any idea what is going on?
Feb 21 '08 #1
3 1862
On Feb 22, 12:15 am, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Thu, 21 Feb 2008 12:34:28 -0800 (PST), "joelcarr...@gmail.com"
<joelcarr...@gmail.comdeclaimed the following in comp.lang.python:
I'm opening up a subprocess like this where slave.py is a text based
app that receives commands and responds with output:
r, w, e = popen2.popen3('python slave.py')
I need to send slave.py a command and see the output,
so I'll do something like:
w.write("command here")
then i'll try this:
w.flush()
A separate thread is reading from r to retrieve output of slave.py.
The problem is that slave.py doesn't seem to receive commands unless I
also do:
w.close()

What happens if you do:

w.write("command here\n")
w.flush()

Could the slave be blocked waiting for an EOL character before
processing said command?
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
I don't think that is the problem, I'm feeding it newline characters.
Feb 22 '08 #2
On Feb 22, 2:01 pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarr...@gmail.com"
<joelcarr...@gmail.comdeclaimed the following in comp.lang.python:
I don't think that is the problem, I'm feeding it newline characters.

It wasn't shown in your sample, so I jumped on the first likely
thing...

The second is in the hands of the subprocess... While you are
flushing output /to/ the subprocess, is IT flushing its output (the
stuff you are trying to read). A common problem seems to be that, as
soon as the process detects a pipe, it goes to buffered I/O, and if the
buffer isn't filled, the parent has no access...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
I'm actually running something like : r, w, e = popen2.popen3('python -
u slave.py')
to try and force unbuffered. slave.py is basically outputting by
using print.
I guess it might still be buffering?
Anyway, thanks for your thoughts... I may have to take an entirely
difference approach. I was hoping not to have to touch the code base
represented by slave.py.
Feb 22 '08 #3
En Fri, 22 Feb 2008 17:53:55 -0200, jo*********@gmail.com
<jo*********@gmail.comescribió:
On Feb 22, 2:01 pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
>On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarr...@gmail.com"
<joelcarr...@gmail.comdeclaimed the following in comp.lang.python:
I don't think that is the problem, I'm feeding it newline characters.

It wasn't shown in your sample, so I jumped on the first likely
thing...

The second is in the hands of the subprocess... While you are
flushing output /to/ the subprocess, is IT flushing its output (the
stuff you are trying to read). A common problem seems to be that, as
soon as the process detects a pipe, it goes to buffered I/O, and if the
buffer isn't filled, the parent has no access...

I'm actually running something like : r, w, e = popen2.popen3('python -
u slave.py')
That was not on your posted example either...
to try and force unbuffered. slave.py is basically outputting by
using print.
I guess it might still be buffering?
Anyway, thanks for your thoughts... I may have to take an entirely
difference approach. I was hoping not to have to touch the code base
represented by slave.py.
[master.py]

import popen2
r, w, e = popen2.popen3('python -u slave.py')

w.write('command 1\n')
w.flush()
print r.readline()
w.write('command 2\n')
w.flush()
print r.readline()
w.write('\n')
w.flush()
print r.readline()

[slave.py]

while True:
line = raw_input().strip()
if not line:
print "bye!"
break
print "echo:",line

That works OK for me on Windows XP.

--
Gabriel Genellina

Feb 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon Spivey | last post by:
Experimenting with response.flush() but can't seem to get it to work. This code <% response.write "<p>started</p>" response.flush() for i = 1 to 10000000 i = i +1 next response.write...
1
by: Victor Irzak | last post by:
Hello, I have a g++-3.3.1 compiler (latest). I can't figure the type of endl, ends, flush. Here is my program. It compiles with VS.NET, Intel C++ compiler, but not with g++. If you can...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
30
by: Jonathan Neill | last post by:
I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge) way of flushing stdin or any other application-level input buffer, but if anyone knows a hack or a non-portable way to...
5
by: Luiz Vianna | last post by:
Guys, I need to send some info to my client while I'm processing some stuff. The flow will be something like : -process -response -process -response .... I imagine to use response.flush...
1
by: Tom | last post by:
Do I really need to flush a streamwriter? It appears to work either way. Dim sw As StreamWriter Dim strFullPath As String = "C:\Temp\Test.txt" If File.Exists(strFullPath) Then sw =...
1
by: nitroamos | last post by:
i'm working on improving the IO for the software project i'm working on to do two extra things. first, i'm going to add HDF5 functionality, and second, add the ability to write binary output. the...
12
by: Daniel Kaplan | last post by:
I was curious about something. In the same table cell I can say this: <A HREF="menu.pl" ><IMG SRC="menu.jpg" width="95" height="35" border="0"></A> <A HREF="help.pl" ><IMG SRC="help.jpg"...
2
by: Damir | last post by:
Hello! I have noticed that after (sucessfully) executing the command: FLUSH PACKAGE CACHE DYNAMIC the dynamic SQL statement cache is not completely cleared (some of the dynamic SQL statement...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.