Connecting to a subprocess stdin to pipe

只愿长相守 提交于 2019-12-13 15:16:10

问题


I have a method that creates a subprocess and connects it STDIN to an anonymous 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)

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. (zenity draws a completion bar graph, reading values from STDIN)

The box gets drawn on the screen, but Screen.progress.write('50') never updates the bar.

What am I doing wrong?

Edit:

If run interactively, as soon as I exit the python shell, the bar starts moving. (pulsing) Which means it read -something- only after the python process exited.


回答1:


os.fdopen() should have a buffer size of 0. Use rfd = os.fdopen(readp, 'r', 0).




回答2:


You probably need to flush the file buffers. Try doing self.progress.flush() after each write.



来源:https://stackoverflow.com/questions/6269886/connecting-to-a-subprocess-stdin-to-pipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!