简体   繁体   中英

Connecting to a subprocess stdin to pipe

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM