Non-blocking reads of a fifo (named pipe)?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Dec 3 23:47:52 UTC 2018


On Mon, Dec 03, 2018 at 11:32:10PM +0000, Anonymouse via Digitalmars-d-learn wrote:
> I have a fifo that I want to read lines from, but everything is
> blocking.
> 
> execute([ "mkfifo", filename ]);
> File fifo = File(filename, "r");  // blocks already
> immutable input = fifo.readln();  // blocks
> foreach (line; fifo.byLineCopy) { /* blocks */ }
> 
> How can I go about doing this to get non-blocking reads? Or perhaps a
> way to test whether there is text waiting in the fifo? File.eof is not
> it.
> 
> if (fifo.hasData)
> {
>     immutable stuff = fifo.readln();
>     // ...
> }

I assume you're using Posix?  In which case, you should take a look at
the `select` or `epoll` system calls. Cf. core.sys.posix.sys.select.

Or possibly `fcntl` (search for O_NONBLOCK), or call `open` directly
with O_NONBLOCK.

There's no common cross-platform way to do this, so generally you have
to write your own code to handle it.


T

-- 
Music critic: "That's an imitation fugue!"


More information about the Digitalmars-d-learn mailing list