[英]Connecting to a subprocess stdin to pipe
I have a method that creates a subprocess and connects it STDIN to an anonymous pipe;我有一个创建子进程并将其 STDIN 连接到匿名 pipe 的方法; which is not working.
这是行不通的。 Its not raising any exceptions, the subprocess just never seems to never read the data.
它没有引发任何异常,子进程似乎永远不会读取数据。 (the subprocess is the 'zenity' executable for displaying a progress bar in the GUI)
(子进程是用于在 GUI 中显示进度条的“zenity”可执行文件)
class Screen(object):
def __init__(self, display = ":0", bin = '/usr/bin/zenity'):
self.bin = bin
os.environ['DISPLAY'] = display
self.dis = display
def displayProgress(self, text, pulse = True, title = 'Progess'):
'''Method to represent the 'zenity --progress' command
'''
readp, writep = os.pipe()
reade, writee = os.pipe()
rfd = os.fdopen(readp, 'r')
wfd = os.fdopen(writep, 'w')
if pulse:
zenhandle = Popen([self.bin, '--progress',
'--title=%s' % title,
'--auto-close',
'--pulsate'],
stdin = rfd)
else:
zenhandle = Popen([self.bin, '--progress',
'--title=%s' % title,
'--auto-close'],
stdin = rfd)
self.progress = wfd
The idea is calling the method will be non-blocking and I can write()
to Screen.progress
and have to written to the STDIN of the child (zenity) process.这个想法是调用该方法将是非阻塞的,我可以
write()
到Screen.progress
并且必须写入子(zenity)进程的 STDIN。 (zenity draws a completion bar graph, reading values from STDIN) (zenity 绘制完成条形图,从 STDIN 读取值)
The box gets drawn on the screen, but Screen.progress.write('50')
never updates the bar.该框被绘制在屏幕上,但
Screen.progress.write('50')
永远不会更新栏。
What am I doing wrong?我究竟做错了什么?
Edit:编辑:
If run interactively, as soon as I exit the python shell, the bar starts moving.如果以交互方式运行,一旦我退出 python shell,条形图就会开始移动。 (pulsing) Which means it read -something- only after the python process exited.
(脉冲)这意味着它仅在 python 进程退出后才读取 -something-。
You probably need to flush the file buffers.您可能需要刷新文件缓冲区。 Try doing
self.progress.flush()
after each write.每次写入后尝试执行
self.progress.flush()
。
os.fdopen()
should have a buffer size of 0. Use rfd = os.fdopen(readp, 'r', 0)
. os.fdopen()
的缓冲区大小应为 0。使用rfd = os.fdopen(readp, 'r', 0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.