I’ve just installed and had some success with the gnuplot library.
I’m saving the output to a png file (plot.terminal “png”) and this all
works
well, but when I try to use the file it isn’t finished writing it yet
and I
get an exception.
I’ve used
Gnuplot.open( ) do |gp|
…
end
while(!File.exists?("/test2.png") || File.zero?("/test2.png") )
sleep 0.1
end
In an attempt to wait until the file is written before I try to do
anything
with it, but this seems a bit too hackish.
Can I capture the Gnuplot.open into a variable and check it? The last
statement in the Gnuplot.open is a call to
IO::popen( cmd, “w”) { |io| yield io }
but if I test the class of what is returned it is
Gnuplot::Plot
not anything to do with IO
Is there a way that I can test the IO::popen to tell when it’s finished?
Gnuplot.open( ) do |gp|
Can I capture the Gnuplot.open into a variable and check it? The last
statement in the Gnuplot.open is a call to
IO::popen( cmd, “w”) { |io| yield io }
but if I test the class of what is returned it is
Gnuplot::Plot
not anything to do with IO
Is there a way that I can test the IO::popen to tell when it’s finished?
can you show the code? that pipe will be automatically closed once the
yield
has returned.
If the IO::popen is creating a child process, how about calling
Process.waitpid? Once that returns, the subprocess will have
completed and the file should be complete.
Cheers,
Chris
On 03 Aug 2006, at 6:29 AM, Daniel N wrote:
I’ve used
anything
Is there a way that I can test the IO::popen to tell when it’s
finished?
If the IO::popen is creating a child process, how about calling
Process.waitpid? Once that returns, the subprocess will have
completed and the file should be complete.
Cheers,
Chris
How would I get the pid that the child process is on?
/test2.png, bizarre as it is, is created correctly at c:\test2.png and I
chart_file = File.open( ‘c:/test2.png’ ) #< raises and exception because
the
file does not yet exist
I would have thought that since Gnuplot.open has IO::popen as it’s last
statement it would return some kind of IO object, but it returns a
Gnuplot::Plot object.
Sorry if I’m going around circles. I appreciate your help with this.
Daniel
I’m really trying to grok how this works.
In the docs for IO it says that the block will return the results of the
block, not an IO object. Sorry I didn’t pick this up earilier.
The other thing that I found in there, I think, was that when the pipe
is
opened, the ruby side of the pipe evaluates the block, sends its results
and
then closes.
I guess this is why I’m having trouble. Ruby executes the block and
closes
then moves onto the next statment, but gnuplot, on the other end of the
pipe
is still doing it’s thing drawing the chart. If it doesn’t finish
before
the ruby moves on I get the exception.
Am I understanding this correctly? If I am, is there any way to get at
the
process that gnuplot is running so I can tell when it’s finished?
the gnuplot exe is not being found in your PATH. search the archives
i’ve solved this one before.
regards.
-a
happiness is not something ready-made. it comes from your own actions.
h.h. the 14th dali lama
Sorry Ara, I must be really thick at this because I don’t understand,
except
the PNG part… whoops
The chart is created, so the exe is found and executed.
/test2.png, bizarre as it is, is created correctly at c:\test2.png and I
can
open it no problem once its written.
The issue is that when I call make_chart and then immediately try to do
something with c:\test2.png it doesn’t exist yet. I need to wait for it
to
be written.
eg
make_chart
chart_file = File.open( ‘c:/test2.png’ ) #< raises and exception because
the
file does not yet exist
I would have thought that since Gnuplot.open has IO::popen as it’s last
statement it would return some kind of IO object, but it returns a
Gnuplot::Plot object.
Sorry if I’m going around circles. I appreciate your help with this.
I’m on windows I don’t need root. Ohhh gives me goosebumps
The problem isn’t that the file isn’t created. It’s just that I
don’t know
when it’s finished being written.
I want to incorporate this into an app but I don’t want to go
trying to grab
the image if it doenst exist yet.
It is possible that the OS is simply not yet finished writing the
file to disk. In unix this is forced by calling fsync (or sync on the
command line). I’ve got no idea what you do in windows.
The first thing to try is to find out what the windows equivalent of
the command line ‘sync’ and run it before trying to open the png. If
that works then you can get fancy…
Assuming the CL sync worked… In ruby the IO class has two methods
defined: fsync which is defined on an instance of the class, and sync
which is defined on the class. IO.sync won’t do what you need, it
just forces ruby to write its buffers. You can try opening the png
file and calling fsync from there, don’t know if that will work.
Alternatively, check to see of gnuplot will either give you the IO
object somehow or can be asked to fsync. If not, you might be able to
sneak into Gnuplot aliasing a method or re-opening the class or some
other trick and insert the fsync.