how to flush a file

Discussion in 'Perl Misc' started by Data Cruncher, Jan 28, 2007.

  1. My process writes to the log file. I want to periodically flush it so
    that tail -f on the the log file shows the progress on a regular
    basis.

    This is what I do to write in the log file:-

    open(LOGFILE,"> file name") or die ...
    print LOGFILE "aaaaa\n" ;
    this print is done during the life of the process.
    What is happening is that until the process terminates
    the log file can not be viewed by tail -f because perl is
    still buffering the output to the log file.
    and then finally
    close(LOGFILE);

    How do I flush it. There is no command like flush(LOGFILE).

    What I have done is a crude approach of closing the log file
    and then reopening it with '>>filename'. As it happens only
    once a minute, it is not that expensive. But I am not happy
    with this approach. It is not an elegant approach.

    TIA.
     
    Data Cruncher, Jan 28, 2007
    #1
    1. Advertising

  2. Data Cruncher

    Dr.Ruud Guest

    Data Cruncher schreef:

    > My process writes to the log file. I want to periodically flush it so
    > that tail -f on the the log file shows the progress on a regular
    > basis.


    This is from `perldoc -f select`:

    $oldfh = select(STDERR); $| = 1; select($oldfh);

    Some programmers may prefer to think of filehandles as objects
    with methods, preferring to write the last example as:

    use IO::Handle;
    STDERR->autoflush(1);

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 28, 2007
    #2
    1. Advertising

  3. Data Cruncher <> wrote:

    > How do I flush it.



    perldoc -q flush

    How do I flush/unbuffer an output filehandle? Why must I do this?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 28, 2007
    #3
  4. Data Cruncher

    Uri Guttman Guest

    >>>>> "A" == Abigail <> writes:

    A> Tad McClellan () wrote on MMMMDCCCXCVIII September
    A> MCMXCIII in <URL:news:>:
    A> && Data Cruncher <> wrote:
    A> &&
    A> && > How do I flush it.
    A> &&
    A> &&
    A> && perldoc -q flush
    A> &&
    A> && How do I flush/unbuffer an output filehandle? Why must I do this?

    A> To be fair to the OP, this FAQ only explains how to unbuffer the handle.
    A> It doesn't explain how to flush a filehandle.

    A> I have to say, flushing a filehandle in Perl is unintuitive, awkward
    A> and phrone to do it while doing unintended side-effects. And, amazingly,
    A> it only takes one line in C.

    that is because you (and the rest of this thread) are confusing
    autoflushing (setting $|) with actual flushing which can be done with
    the flush call in IO::Handle. it used to be fflush in POSIX but that
    refers to IO::Handle now. so for older perls look for fflush in POSIX.

    setting $| may not be a desirable thing as it will flush for each
    print. sometimes you do want to let buffering work for you and flush as
    needed. i prefer to buffer stuff myself with .= into a string and use
    syswrite (or File::Slurp::write_file). it has many benefits including
    more control over where and when you write.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 28, 2007
    #4
  5. Data Cruncher

    Dr.Ruud Guest

    Abigail schreef:
    > Dr.Ruud:
    >> Data Cruncher:


    >>> My process writes to the log file. I want to periodically flush
    >>> it so that tail -f on the the log file shows the progress on a
    >>> regular basis.

    >>
    >> This is from `perldoc -f select`:
    >>
    >> $oldfh = select(STDERR); $| = 1; select($oldfh);

    >
    > Well, that will flush STDERR but it has the effect of leaving it
    > unbuffered. Which may not be what the OP wants.


    OP wrote "periodically flush it", so you are right that leaving it
    buffered in between can be what he wants.


    > I'd write it as:
    >
    > my $old_fh = select STDERR; {local $| = 1;} select $old_fh;


    Yes, that is a nice and safe way to do a (single) flush, and restore the
    original autoflush-status.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 28, 2007
    #5
  6. Data Cruncher

    Bo Lindbergh Guest

    In article <290120071540480925%>,
    Jim Gibson <> wrote:
    > But nowhere in the documentation does it say that setting auto-flush
    > mode for a filehandle results in an immediate flush of that filehandle.


    But perldoc perlvar says:
    > $| If set to nonzero, forces a flush right away and after every
    > write or print on the currently selected output channel.


    What part of that sentence is unclear?


    /Bo Lindbergh
     
    Bo Lindbergh, Jan 30, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina

    Re: Response.Flush() in code-behind

    Marina, Jul 1, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    William F. Robertson, Jr.
    Jul 1, 2003
  2. William F. Robertson, Jr.

    Re: Response.Flush() does not work

    William F. Robertson, Jr., Aug 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    521
    William F. Robertson, Jr.
    Aug 5, 2003
  3. Anon Anon
    Replies:
    4
    Views:
    2,560
    Mike Treseler
    May 29, 2007
  4. Replies:
    1
    Views:
    1,962
  5. MRAB
    Replies:
    5
    Views:
    266
Loading...

Share This Page