Re: how many chars/bytes does STDOUT buffer (test)
by tye (Sage) on Apr 02, 2015 at 13:35 UTC
|
$ perl -MTime::HiRes=sleep -e'while(1){print ".";sleep 0.001}' | perl
+ -le'print sysread(STDIN,$rec,1024*1024)'
4096
(no, the sleep isn't strictly necessary, but it makes the buffering effect more obvious.)
| [reply] [d/l] |
|
|
I'm getting 8192. Perl 5.14.4 on Cygwin 64.
| [reply] |
|
|
| [reply] |
|
|
Yeah, the point was the test, not the sample number produced. Since "line-buffer STDOUT to a terminal but full-buffered to a file or pipe" is the pattern for almost all programs, not just Perl, it is likely that the buffering is not done by Perl and so the size depends on your OS and/or C library version. Though, Perl has several choices of I/O layer implementation and one or more of them might re-implement that standard I/O buffer behavior but might choose different default sizes (though, I recall there being ways to specify buffering and the standard http://perl.plover.com/FAQs/Buffering.html documents such and much more).
| [reply] |
|
|
|
|
|
Re: how many chars/bytes does STDOUT buffer
by MidLifeXis (Monsignor) on Apr 02, 2015 at 13:40 UTC
|
IIRC, it is system dependent, but is usually some multiple of an IO Block Size.
| [reply] |
Re: how many chars/bytes does STDOUT buffer
by sandy105 (Scribe) on Apr 03, 2015 at 06:29 UTC
|
| [reply] |
|
|
It's not. See BrowerUk's answer.
| [reply] |
|
|
Yes, Perl does have its own buffer, in addition to whatever buffering the operating system may or may not do. The safest thing to do is to assume that the size of the buffer is unpredictable. If you need to explicitly flush it, do so, but do not build-in any assumptions as to how large it is.
| [reply] |
|
|
in addition to whatever buffering the operating system may or may not do.
The OS may delay writing it to disk a little after receiving it, but it will appear to be on the disk to anyone trying to read the file immediately after Perl sends it to the OS.
That means that if you you use flush or autoflush to send the data to the OS, it will be immediately available to everyone, though the data could be lost if a power outage occurs shortly after.
| [reply] [d/l] [select] |