www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Non-blocking reads of a fifo (named pipe)?

reply Anonymouse <asdf asdf.net> writes:
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();
     // ...
}
Dec 03 2018
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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!"
Dec 03 2018
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Monday, 3 December 2018 at 23:32:10 UTC, Anonymouse wrote:
 I have a fifo that I want to read lines from, but everything is 
 blocking.
 [...]
 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.
 [...]
I did this i think for my AsyncProcess class. It was a while ago and i never used it but IIRC it seemed to work on linux (not on Windows still IIRC) https://github.com/BBasile/iz/blob/master/import/iz/classes.d#L1584
Dec 03 2018