|
From: Hendrik M. <Hen...@we...> - 2003-03-17 11:05:27
|
Hi,
the problem is described in the FAQ of the gnuplot-package.
(You will get the same problem if you use Gnuplot in function which returns
before gnuplot is finished, for example:
def makeimage(data, file):
g=Gnuplot.Gnuplot()
g('set term png')
g('set output \'' + file+ '\'')
g.plot(data)
return
you will get something like this:
gnuplot> plot '/tmp/@24463.2'
^
can't read data file "/tmp/@24463.2"
line 0: (No such file or directory)
)
A possible fix would be to use popen2(available on unix and windows) instead
of os.popen and to define __del__(self) to savely shutdown the gnuplot
process.
the last part of my gp_unix.py looks like this:
#from os import popen
from popen2 import Popen3
def test_persist():
"""Determine whether gnuplot recognizes the option '-persist'.
If the configuration variable 'recognizes_persist' is set (i.e.,
to something other than 'None'), return that value. Otherwise,
try to determine whether the installed version of gnuplot
recognizes the -persist option. (If it doesn't, it should emit an
error message with '-persist' in the first line.) Then set
'recognizes_persist' accordingly for future reference.
"""
if GnuplotOpts.recognizes_persist is None:
import string
g = Popen3('echo | %s -persist 2>&1' % GnuplotOpts.gnuplot_command,
'r')
response = g.readlines()
g.close()
GnuplotOpts.recognizes_persist = (
(not response) or (string.find(response[0], '-persist') == -1))
return GnuplotOpts.recognizes_persist
class GnuplotProcess:
"""Unsophisticated interface to a running gnuplot program.
This represents a running gnuplot program and the means to
communicate with it at a primitive level (i.e., pass it commands
or data). When the object is destroyed, the gnuplot program exits
(unless the 'persist' option was set). The communication is
one-way; gnuplot's text output just goes to stdout with no attempt
to check it for error messages.
Members:
'gnuplot' -- the pipe to the gnuplot command.
Methods:
'__init__' -- start up the program.
'__call__' -- pass an arbitrary string to the gnuplot program,
followed by a newline.
'write' -- pass an arbitrary string to the gnuplot program.
'flush' -- cause pending output to be written immediately.
"""
def __init__(self, persist=None):
"""Start a gnuplot process.
Create a 'GnuplotProcess' object. This starts a gnuplot
program and prepares to write commands to it.
Keyword arguments:
'persist=1' -- start gnuplot with the '-persist' option,
(which leaves the plot window on the screen even after
the gnuplot program ends, and creates a new plot window
each time the terminal type is set to 'x11'). This
option is not available on older versions of gnuplot.
"""
if persist is None:
persist = GnuplotOpts.prefer_persist
if persist:
if not test_persist():
raise ('-persist does not seem to be supported '
'by your version of gnuplot!')
self.gnuplot = Popen3('%s -persist' % GnuplotOpts.gnuplot_command,
'w')
else:
self.gnuplot = Popen3(GnuplotOpts.gnuplot_command, 'w')
# forward write and flush methods:
self.write = self.gnuplot.tochild.write
self.flush = self.gnuplot.tochild.flush
def __call__(self, s):
"""Send a command string to gnuplot, followed by newline."""
self.write(s + '\n')
self.flush()
def __del__(self):
self.write('exit\n')
self.flush()
self.gnuplot.wait()
Regards,
Hendrik Muhs
P.S. for answers: Please CC me, because I am not subscribed to this list
|