# Stdout buffering

**URL:** <https://discourse.julialang.org/t/stdout-buffering/111182>\
**Category:** General Usage\
**Tags:** documentation\
**Created:** [March 5, 2024, 10:07am UTC](https://discourse.julialang.org/t/stdout-buffering/111182 "2024-03-05T10:07:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [March 5, 2024, 10:07am UTC](https://discourse.julialang.org/t/stdout-buffering/111182/1 "2024-03-05T10:07:02Z")

</div>

I seem to fail to RTFM with stream buffering.

My problem is the following (under linux):

```julia
$ strace -o trace stdbuf -o 1MB julia -e "for i=1:100000 write(stdout,\"something\") end"| cat >/dev/null
$ wc -l trace 
301126 trace

```

So this clearly failed to buffer.

Can anyone help me with this? My questions would be:

1. Am I doing something wrong?
2. How do I get a buffered stream?
3. Where is this in the documentation? Such that I next time know where to look

If julia doesn’t respect the OS settings for buffering, then it’s also OK if I can change the stdout buffering from within julia.

PS. I believe the relevant documentation belongs into

```julia
help?> stdout
search: stdout redirect_stdout

  stdout::IO

  Global variable referring to the standard out stream.

```

The documentation should explain:

1. How does the way of starting julia affect the concrete type of `stdout`? (tty, pipe, file)
2. How is the buffering determined from outside julia? (environment variables, cmd flags, tools like `stdbuf` and if necessary analogues on bsd + macos + windows)
3. How can you check and change buffering mode from within julia?

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [March 5, 2024, 10:20am UTC](https://discourse.julialang.org/t/stdout-buffering/111182/2 "2024-03-05T10:20:57Z")

</div>

See:

> <https://github.com/JuliaLang/julia/issues/13050>
>
> When 0.4-dev Julia script is submitted to a job manager (SGE), and STDERR/STDLOG… is redirected to a file, this log file is updated very infrequently, approx. once in 3 hours. Is there a way to increase the updates rate?
> 
> The script looks like this:
> 
> \`\`\` bash
> \#!/bin/sh
> \#$ -pe openmp 16
> \#$ -R y
> \#$ -l h\_rt=11:00:00
> \#$ -l h\_vmem=4G
> \#$ -S /bin/bash
> \#$ -m ae
> \#$ -j y
> \#$ -o \<...\>/$JOB\_NAME\_$JOB\_ID.log
> 
> \<...\>
> 
> export NWORKERS=$(($NSLOTS-1))
> 
> julia --color=no -q -p $NWORKERS \<...\>/run.jl \<...\>
> \`\`\`

and this thread:

> [@Set flushing mode for output stream](https://discourse.julialang.org/t/set-flushing-mode-for-output-stream/102546/3):
>
> I think the best way is to Define default buffering for stdout and stderr. On Linux, stdout is line-buffered and stderr is unbuffered (I haven’t verified this, though). I think these are excellent defaults. Give the programmer the ability of setting the buffering behavior of a new stream. Give the programmer the ability of changing the buffering mode of any stream. I’m afraid I don’t think that flushing in println() is the way to go. We expect maximal efficiency when printing into a fully b…

maybe of help, I tried to understand the issues, and make a PR, I probably need to reread (what I wrote), posting in case helpful.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [March 5, 2024, 10:50am UTC](https://discourse.julialang.org/t/stdout-buffering/111182/3 "2024-03-05T10:50:16Z")

</div>

Found it:

```julia
$ strace -o trace julia -e "Base.buffer_writes(stdout, 1<<20); for i=1:100000 write(stdout,\"something\") end"| cat >/dev/null
$ wc -l trace 
1095 trace

```

The way I found it was:

1. First, figure out what I’m dealing with: `$ julia -e "@show stdout"|cat stdout = Base.PipeEndpoint(RawFD(12) open, 0 bytes waiting) `
2. Read all uses of `PipeEndPoint` in Base. Nothing ☹
3. Read uses of `Base.LibuvStream` in Base until I found paydirt.

And yet,

```julia
help?> Base.buffer_writes
  No documentation found.

```
