tMC
tMC

Reputation: 19355

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.

Upvotes: 3

Views: 2013

Answers (2)

Christopher Dolan
Christopher Dolan

Reputation: 179

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

Upvotes: 1

Rakis
Rakis

Reputation: 7874

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

Upvotes: 1

Related Questions