Search code examples
pythonlinuxubuntubufferingstdio

How can I flush the linux stdio-redirect-buffer of an already running python program?


I got a long running program that I don't want to kill, but I do want to see the output that it has (which is one sentence every hour or so).

I know now that I needed to flush the output in my script (e.g. sys.stdout.flush()), but since I don't want to kill it, I'm hoping there is some clever linux command to do this. From what I've read online, the typical linux behavior is to flush this when the buffer fills up.

If nobody knows answer, do you have any idea of how big this buffer is? It would be nice to at least get an idea of when to check back :)

[this is on Ubuntu 10]


Solution

  • To find out the buffer size for sys.stdout in Ubuntu 11.10 I used the following simple program:

    import sys, time
    
    buf_size = 1024
    for counter in xrange(buf_size):
        sys.stdout.write('a')
    time.sleep(3)
    

    The experiment is as follows:

    • If buf_size is too big, part of the string is printed immediately to the console and the remaining part after three seconds.
    • If buf_size is too small, the whole output is printed after three seconds.

    In my case, when the buffer size is set to 1024, I don't get any output until the program exits, but when I set it to 1025 I see a lot of characters and, after three seconds, one more character is printed.

    Hence, the buffer size in my system is 1024. In your system is probably the same, but you can repeat the experiment to confirm.

    For the future, please find below some links that can be helpful: