I can't really find much information out there that mentions this. Are these standard io descriptors setup to be buffered or unbuffered by default in Lua? Can I switch from one mode to the other if desired?
For instance, in python it provides something like sys.stdin.detach
for making it unbuffered.
Unbuffered stream invokes a system call on every read or write . Whereas line buffered stream invokes a system call on every line(whenever the buffer encounters a newline). Also, stdin and stdout are both standard I/O stream and are typically line buffered.
Notes. The stream stderr is unbuffered. The stream stdout is line-buffered when it points to a terminal. Partial lines will not appear until fflush(3) or exit(3) is called, or a newline is printed.
By default writes to stdout pass through a 4096 byte buffer, unless stdout happens to be a terminal/tty in which case it is line buffered.
You can use the setvbuf function: setvbuf(stdout, NULL, _IONBF, 0);
See setvbuf. It is an interface to the underlying C setvbuf
function.
For example you can use it like this:
io.stdout:setvbuf 'no' -- switch off buffering for stdout
AFAIK Lua relies on the underlying C runtime to hook into standard streams, therefore I think the usual guarantees for C standard streams apply.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With