Search code examples
pythonfuse

Python print to stderr in generator


Building a python fuse fs, in my readdir generator the first line of code is a print statement. This never appeared on my console. I modified it to a print to stderr as i thought it was a buffering issue. Still no output.

I added a manual flush to the next line - still nothing.

I added a time.sleep(3) to the next line, the prog does indeed sleep.

def readdir(self, path, offset):
    print >> sys.stderr, 'Text'
    sys.stderr.flush()
    time.sleep(3)

I then go on to populate the directory with other code (yield fuse.Direntry) I do get output and can do an ls in the terminal to see the contents of my mounted fuse directory, but I want to know why the print command doesn't work in this one generator.

Update

For those that are struggling :

def readdir(self, path, offset):
    print >> sys.stderr, 'Text'
    sys.stderr.flush()
    for o in os.listdir( "." + path ):
        yield fuse.Direntry(o)

Is the code. It generates a file list and I can move around it. That is fine. The problem is I never see 'Text' appear, not in STDOUT, STDERR, anywhere.

I was simply asking why this happens only in this generator. I can put print elsewhere in the fuse code and get output very well.


Solution

  • Your FUSE code runs under the control of FUSE library, so you can't be sure that stdin, stdout and stderr are the ones in your terminal session. It's quite possible that the readdir generator is called with alternate std* streams.

    Since you say that other parts of your FUSE code print without any issues, you might want to prefix all print statements with a debugging log message (the value of sys.stdout.isatty() or even the repr(sys.stdout) could be handy) to help you understand what is going on.