1
我想通过python运行gnuplot。Python子进程标准输出不读取
我可以发送和运行命令,但无法读取应用程序中的警告或错误消息。它只是在这里等待:“self.proc.stdout.readline()”。
这里是我的全部代码:
from subprocess import PIPE, Popen
import fcntl, os
class Gnuplot:
def __init__(self, debug=True):
self.debug = debug
if self.debug:
print 'Initializing ...\n'
self.proc = Popen(['gnuplot','-persist'],stdin=PIPE, stdout=PIPE, stderr=PIPE)
fcntl.fcntl(self.proc.stderr.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
def communicate(self, cin='\n'):
self.proc.stdin.write(cin+'\n')
cout, cerr = '', ''
print "lol"
if self.proc.stdout:
cout = self.proc.stdout.readline()
self.proc.stdout.close()
print cout
elif self.proc.stderr:
cerr = self.proc.stderr.read()
self.proc.stderr.close()
print cerr
if __name__ == '__main__':
g = Gnuplot()
g.communicate("set parameter\n")
g.communicate("plot sin(x)\n")
它只是等待在这里:
cout = self.proc.stdout.readline()