【发布时间】:2017-11-20 10:21:16
【问题描述】:
我正在尝试将 gnuplot 的包装器从 python2 移植到 python3。大多数错误都很容易修复,但与项目的通信似乎表现出意外。我已经在以下(丑陋的)sn-p 中隔离了问题。
cmd = ['gnuplot']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.stdin.write("set terminal dumb 80 40\n")
p.stdin.write("plot '-' w p ls 1, '-' w p ls 2, '-' w p ls 3 \n")
p.stdin.write("1 2 3\n")
p.stdin.write("2 3 4\n")
p.stdin.write("\ne\n")
p.stdin.write("e\n")
p.stdin.write("e\n")
while True:
print(p.stdout.read(1),end="")
此代码在 python2 中有效并生成并打印结果,但在 python3 中失败。首先它抱怨字节和字符串,所以我添加universal_newlines=True。从那里我无法理解为什么它在 stdout 上不输出任何内容并在 stderr 中打印:
line 4: warning: Skipping data file with no valid points
line 5: warning: Skipping data file with no valid points
显然问题出在编码或通信的某个地方,因为我发出的命令是相同的,但我不知道在哪里查看或如何调试它。
欢迎提出任何建议。
【问题讨论】:
-
将其设为
cmd = ['tee', 'logfile'],并逐字节比较两个logfile。 -
顺便说一句,我强烈建议在输入结束时使用
p.stdin.flush()或p.stdin.close()。 -
并且你应该确保对 stderr 的写入不会阻塞等待读取它们的东西,如果你要拥有
stderr=subprocess.PIPE- 你很容易陷入僵局。 -
尝试
bufsize=1使用universal_newlines=True启用行缓冲(默认情况下,Python 2 上为 bufsize=0,Python 3 上默认为 bufsize=-1)。
标签: python python-2.7 python-3.5