guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reading from input pipe is slow?


From: Mark H Weaver
Subject: Re: reading from input pipe is slow?
Date: Fri, 22 Jun 2018 17:47:42 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Hi Thomas,

Thomas Danckaert <address@hidden> writes:

> I've notice that reading a child process' stdout from a port opened
> with (open-input-pipe "command") is very slow.  In fact, it's faster
> to redirect the child process' output to a temporary file, and then
> read this temporary file from Guile, than to directly read the child
> process' output using a port (I've added some example code below where
> you can see this).
>
> Is there an explanation for this?  Or even better: a solution?

For some reason that I don't know, possibly historical, the pipes
created by procedures in (ice-9 popen) are unbuffered by default.  When
reading or writing from an unbuffered port, a system call is made for
every byte.

To enable buffering on pipe ports, call (setvbuf port 'block) after
opening it.  Something like this, where (rnrs io ports) is added to the
module imports you already had, for 'call-with-port':

  (call-with-port (open-input-pipe my-command)
    (lambda (port)
      (setvbuf port 'block)
      (string-take-right (get-string-all port)
                         15)))

Hope this helps,

      Mark



reply via email to

[Prev in Thread] Current Thread [Next in Thread]