调用gnuplot然后在新创建的文件上显示的Python subprocess.call()有时没有输出

调用gnuplot然后在新创建的文件上显示的Python subprocess.call()有时没有输出,python,subprocess,gnuplot,Python,Subprocess,Gnuplot,我一直在尝试使用Python3.4调用一个简单的gnuplot脚本,然后使用evice显示结果(一个.ps文件)。我的代码如下所示: plotfile = "plot.p" with open(plotfile, "w") as plt: lines = ['set term postscript', 'set output "speed.ps"', 'set xlabel "Time"', 'set yla

我一直在尝试使用Python3.4调用一个简单的gnuplot脚本,然后使用evice显示结果(一个.ps文件)。我的代码如下所示:

plotfile = "plot.p"

with open(plotfile, "w") as plt:
    lines = ['set term postscript',
             'set output "speed.ps"',
             'set xlabel "Time"',
             'set ylabel "Speed"',
             "plot '{}' u 2:4 title 'speed' with lines".format(infile)
            ]
    lines = map(lambda x: x + '\n', lines)
    plt.writelines(lines)

    # Call gnuplot and evince
    subprocess.call(['gnuplot', plotfile])
    subprocess.call(['evince', 'speed.ps'])
但是,
evice
通常不显示文件。我可以使用

process = subprocess.Popen(['gnuplot', plotfile])  
但是如果我试图立即打开文件,通过调用

process = subprocess.Popen(['gnuplot', plotfile])  
subprocess.Popen(['evince', plot.ps])
我假设文件经常不能正确显示,因为第一个命令没有及时完成。为了解决这个问题,我尝试了

process = subprocess.call(['gnuplot', plotfile])

但在这两种情况下,甚至都没有创建.ps文件。唯一有效(但并非总是有效)的是

但这真的很难看

因此,我的问题是:

1) 为什么等待进程完成(使用
subprocess.call()
wait()
)会阻止创建.ps文件 2) 有没有不使用“睡眠”的解决方案

版本:

  • CentOS 6.6
  • Python 3.4
  • Gnuplot 4.6
  • 证据2.28.2

我相信你有一些误导性的测试结果,导致你走上了错误的道路。对
Popen
对象使用
subprocess.call()
或调用
.wait()
确实会等待被调用的程序退出。但是,无法保证的是,在启动
gnuplot
时,
plt.writelines()
调用的结果是否会刷新到磁盘,或者仍然在内存缓冲区中

给定以下代码:

plotfile = "plot.p"

with open(plotfile, "w") as plt:
    lines = ['set term postscript',
             'set output "speed.ps"',
             'set xlabel "Time"',
             'set ylabel "Speed"',
             "plot '{}' u 2:4 title 'speed' with lines".format(infile)
            ]
    lines = map(lambda x: x + '\n', lines)
    plt.writelines(lines)

    plt.flush() ### <-- EITHER CALL THIS OR MOVE THE BELOW OUTSIDE THE with BLOCK

    # Call gnuplot and evince
    subprocess.call(['gnuplot', plotfile])
    subprocess.call(['evince', 'speed.ps'])
plotfile=“plot.p”
打开(绘图文件“w”)作为plt:
行=['set term postscript',
'设置输出“速度.ps”',
“设置xlabel“时间”,
'设置标签“速度”',
“用线条绘制“{}”u 2:4标题“速度”。格式(内嵌)
]
lines=map(λx:x+'\n',lines)
打印行(行)

plt.flush()#####我相信你有一些误导性的测试结果,导致你走错了路。对
Popen
对象使用
subprocess.call()
或调用
.wait()
确实会等待被调用的程序退出。但是,无法保证的是,在启动
gnuplot
时,
plt.writelines()
调用的结果是否会刷新到磁盘,或者仍然在内存缓冲区中

给定以下代码:

plotfile = "plot.p"

with open(plotfile, "w") as plt:
    lines = ['set term postscript',
             'set output "speed.ps"',
             'set xlabel "Time"',
             'set ylabel "Speed"',
             "plot '{}' u 2:4 title 'speed' with lines".format(infile)
            ]
    lines = map(lambda x: x + '\n', lines)
    plt.writelines(lines)

    plt.flush() ### <-- EITHER CALL THIS OR MOVE THE BELOW OUTSIDE THE with BLOCK

    # Call gnuplot and evince
    subprocess.call(['gnuplot', plotfile])
    subprocess.call(['evince', 'speed.ps'])
plotfile=“plot.p”
打开(绘图文件“w”)作为plt:
行=['set term postscript',
'设置输出“速度.ps”',
“设置xlabel“时间”,
'设置标签“速度”',
“用线条绘制“{}”u 2:4标题“速度”。格式(内嵌)
]
lines=map(λx:x+'\n',lines)
打印行(行)

plt.flush()####你不能改为在gnuplot命令行中传递脚本吗?“甚至没有创建.ps文件”——开始的地方是找出在这种情况下失败的原因(因为这里给出的代码应该在这种情况下工作),而不是继续尝试其他事情。不幸的是,除非您提供一个(例如,这意味着实际检索或创建绘图文件的代码),否则我们将无法帮助您实现这一点。如果您的绘图文件(例如)被一个尚未显式刷新的仍然打开的文件句柄引用,或者类似的内容引用,我一点也不会感到惊讶,但这类事情纯属猜测,除非您真的为我们提供了完整的、自包含的代码(在隔离运行时经过验证会导致问题),否则我们可以自己查看问题并验证建议的修复。@CharlesDuffy,我认为文件句柄的解释实际上是完全正确的。我是在打开“绘图文件”的情况下完成所有这些的。我没有意识到这会导致子流程出现问题。你能解释一下为什么会这样吗?非常感谢。我添加了更多的代码来说明问题所在。你不能改为在gnuplot命令行中传递脚本吗?“甚至没有创建.ps文件”--开始的地方是找出在这种情况下失败的原因(因为这里给出的代码应该在这种情况下工作),而不是继续尝试其他事情。不幸的是,除非您提供一个(例如,这意味着实际检索或创建绘图文件的代码),否则我们将无法帮助您实现这一点。如果您的绘图文件(例如)被一个尚未显式刷新的仍然打开的文件句柄引用,或者类似的内容引用,我一点也不会感到惊讶,但这类事情纯属猜测,除非您真的为我们提供了完整的、自包含的代码(在隔离运行时经过验证会导致问题),否则我们可以自己查看问题并验证建议的修复。@CharlesDuffy,我认为文件句柄的解释实际上是完全正确的。我是在打开“绘图文件”的情况下完成所有这些的。我没有意识到这会导致子流程出现问题。你能解释一下为什么会这样吗?非常感谢。我添加了更多的代码来演示问题所在。
plotfile = "plot.p"

with open(plotfile, "w") as plt:
    lines = ['set term postscript',
             'set output "speed.ps"',
             'set xlabel "Time"',
             'set ylabel "Speed"',
             "plot '{}' u 2:4 title 'speed' with lines".format(infile)
            ]
    lines = map(lambda x: x + '\n', lines)
    plt.writelines(lines)

    plt.flush() ### <-- EITHER CALL THIS OR MOVE THE BELOW OUTSIDE THE with BLOCK

    # Call gnuplot and evince
    subprocess.call(['gnuplot', plotfile])
    subprocess.call(['evince', 'speed.ps'])