janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.38k stars 217 forks source link

A way to read standard input in the background without `file/read` in a thread. #1398

Open amano-kenji opened 4 months ago

amano-kenji commented 4 months ago

Right now, stdin is a file. I have to use file/read in a thread.

I can't kill the program with (os/exit 1) which is blocked by file/read.

llmII commented 4 months ago

I don't think Janet has a way at the moment, but I seem to recall one can close all the std files, then reopen them by file descriptors. If there was a way to operate `/openon file descriptors (integers) for I think FD 0, 1, and 2 - that would solve the issue. If what was opened was returned as a stream instead of a file (like howos/open` does) then it'd be async-able.

bakpakin commented 4 months ago

Building on this, I have recently pushed code that extends os/exit with a force parameter which allows calling _exit instead of exit. This circumvents the issue where exit needs to flush the standard streams before completing, which in some cases may fail. Unfortunately, this seems to be required by the C standard as detailed here - https://en.cppreference.com/w/c/program/exit ("all C streams are flushed and closed") The async streams though may be the better solutions.

amano-kenji commented 4 months ago

Is it difficult to implement stdin as core/stream?

amano-kenji commented 4 months ago

(os/exit 1 true) can be a way to solve this issue, too.