I have found quite a bit on the web regarding asynchronous IO, but
nothing really works the way I expected it to in my case.
I’m new to this but herewith my question anyway:
Currently I’m using OI.popen to open a new pipe to another process, and
I feed commands to it; but sometimes the other process could run for
multiple minutes before closing the pipe; and only at that stage can I
then see the output. Now, what I really need is to see the output of the
process as it writes to stdout already and not only when closing the
pipe?
Currently I’m using OI.popen to open a new pipe to another process, and
I feed commands to it; but sometimes the other process could run for
multiple minutes before closing the pipe; and only at that stage can I
then see the output. Now, what I really need is to see the output of the
process as it writes to stdout already and not only when closing the
pipe?
The problem you’re probably facing is due to the program being run via
popen buffering its output. This is pretty normal when the program is
outputting to a pipe, as it does when run via popen.
This page has a reasonable summary of things:
Unless the program you’re trying to run has some specific options
available to force it to disable buffering, you’re probably going to be
forced to write your own sort of popen method that allocates a pty
rather than a pipe for interaction with the program.
Currently I’m using OI.popen to open a new pipe to another process, and
I feed commands to it; but sometimes the other process could run for
multiple minutes before closing the pipe; and only at that stage can I
then see the output. Now, what I really need is to see the output of the
process as it writes to stdout already and not only when closing the
pipe?
I just so happen to have been mucking about with the PTY
(pseudoterminal) library under ruby 1.9.3.
A pseudoterminal might be what you need if you can’t change the
buffering of the programs you’re trying to manage.
You’ll need to play with the options to get the result you want -
there’s no really generic way to automatically manage interactive
programs that expect to interact with a person.
I’ve found it useful to run the test script in one terminal with the
–debug option and in another use
tail -F dbg.log
to see exactly what is passing between the parent and the child process.
As an example you could look at the differences between: