non-block reading from pipe stdout

Oleg B code.viator at gmail.com
Tue Oct 3 12:20:09 UTC 2017


On Tuesday, 3 October 2017 at 10:45:21 UTC, kdevel wrote:
> On Tuesday, 3 October 2017 at 00:22:28 UTC, Oleg B wrote:
>> but get error "Resource temporarily unavailable".
>
> You get EAGAIN because there is no data available at the time 
> of reading.
>
> From the manpage of read:
>
> ERRORS
>        EAGAIN Non-blocking  I/O has been selected using 
> O_NONBLOCK and no data
>               was immediately available for reading.

I found only one way: C-style

         auto pp = pipeShell(updaterScriptCommand, Redirect.all,
                             null, Config.none, workDir);

         import core.sys.posix.unistd : read;
         import core.stdc.errno;
         import core.sys.posix.fcntl;

         int fd = pp.stdout.fileno;
         int flags = fcntl(fd, F_GETFL, 0);
         flags |= O_NONBLOCK;
         fcntl(fd, F_SETFL, flags); // C-style setting file config

         char[256] buf;
         while (!tryWait(pp.pid).terminated)
         {
             auto cnt = read(fd, buf.ptr, buf.length); // C-style 
reading
             if (cnt == -1 && errno == EAGAIN) // C-style error 
checking
                 yield();
             else if (cnt > 0)
             {
                 doSomething(buf[0..cnt]);
                 yield();
             }
         }


More information about the Digitalmars-d-learn mailing list