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

Re: Running external programs asynchronously with no need to handle output?



Quoting Hannu Koivisto:
> How can I run external programs so that /dev/null (or something
> equivalent on Windows etc. -- this applies to further references to
> /dev/null too) is used as their standard input and either their
> output goes to /dev/null or to the standard output of the MzScheme
> process?  I.e. something like process*/ports but I don't want to
> construct /dev/null file-stream port for input myself

To /dev/null:

You'll have to construct the port by opening "/dev/null" with
`open-input-file' or `open-output-file'. Supply the resulting port to
process*/ports.

You can close the port immediately when `process/ports' returns,
because the process has its own copy of the file descriptor.
(The documentation doesn't say that, but it should.)

If I remember correctly, the equivalent of "/dev/null" in Windows is
"NUL".

To stdout:

In plain MzScheme, `current-output-port' gives you a file-stream port.

In MrEd or DrScheme, it's trickier. I think you have to create a thread
that copies the output from the process to the current output port.
Something like this:

(define (copy-to-stdout src)
  (thread (lambda ()
            (let ([s (make-string 4096)])
              (let loop ()
                (let ([l (read-string-avail! s src)])
                  (unless (eof-object? l)
                    (display (if (< l 4096) (substring s 0 l) s))
                    (loop))))
              (close-input-port src)))))

This will get better soon. When the module conversion is complete, I'll
revise `process' et al. with code from Eli, and we'll make
process/ports work with arbitrary ports (by creating the necessary
threads automatically).

Matthew