Haskell Conduit от процесса захвата как stdout, так и stderr
Есть ли канал Haskell, который может выполнить процесс и захватить как его stderr
а также stdout
потоки (отдельно)? Способность пройти в stdin
чтобы процесс был бы идеальным, так как проводник тоже был бы идеальным, но не требованием (я могу использовать файл для этого).
1 ответ
Вот пример, полученный из статьи Data.Conduit.Process школы Haskell:
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative ((*>))
import Control.Concurrent.Async (Concurrently (..))
import Data.Conduit (await, yield, ($$), (=$))
import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.List as CL
import Data.Conduit.Process (ClosedStream (..), streamingProcess,
proc, waitForStreamingProcess)
import System.IO (stdin)
main :: IO ()
main = do
putStrLn "Enter lines of data. I'll run ./base64-perl on it."
putStrLn "Enter \"quit\" to exit."
((toProcess, close), fromProcess, fromStderr, cph) <-
streamingProcess (proc "./base64-perl" [])
let input = CB.sourceHandle stdin
$$ CB.lines
=$ inputLoop
=$ toProcess
inputLoop = do
mbs <- await
case mbs of
Nothing -> close
Just "quit" -> close
Just bs -> do
yield bs
inputLoop
output = fromProcess $$ CL.mapM_
(\bs -> putStrLn $ "from process: " ++ show bs)
errout = fromStderr $$ CL.mapM_
(\bs -> putStrLn $ "from stderr: " ++ show bs)
ec <- runConcurrently $
Concurrently input *>
Concurrently output *>
Concurrently errout *>
Concurrently (waitForStreamingProcess cph)
putStrLn $ "Process exit code: " ++ show ec
В основном это пример из статьи с добавлением потока для обработки stderr.
Он вызывает эту Perl-программу, которая выдает выходные данные как в stdout, так и в stderr:
#!/usr/bin/env perl
use strict;
use warnings;
use MIME::Base64;
$| = 1;
my $timeout = 3;
my $buf = "";
while (1) {
my $rin = '';
vec($rin, fileno(STDIN), 1) = 1;
my ($nfound) = select($rin, undef, undef, $timeout);
if ($nfound) {
my $nread = sysread(STDIN, $buf, 4096, length($buf));
last if $nread <= 0;
print encode_base64($buf);
$buf = "";
} else {
print STDERR "this is from stderr\n";
}
}