0
如何擴展「Running processes concurrently」以包含stdin和stdout?與stdin同時運行進程stdout
例如,讓我們說,我的(Windows)中的命令輸出10 stdout和我要檢查所有進程的輸出是正確的:
let cmds = replicate 100 "echo 10"
我應該怎麼寫Haskell的計劃?
如何擴展「Running processes concurrently」以包含stdin和stdout?與stdin同時運行進程stdout
例如,讓我們說,我的(Windows)中的命令輸出10 stdout和我要檢查所有進程的輸出是正確的:
let cmds = replicate 100 "echo 10"
我應該怎麼寫Haskell的計劃?
import Control.Concurrent.Async
import System.IO
import System.Process
你鏈接的網站上的代碼使用runCommand
;使您能夠訪問流的等價物是runInteractiveCommand
。然後您可以使用hGetContents
從流中讀取。
-- | Run @echo [email protected] and tests whether the output is what we expect.
testOne :: IO Bool
testOne =
runInteractiveCommand "echo 10" >>= \(_stdin, stdout, _stderr, _proc) ->
hGetContents stdout >>= \out ->
return (out == "10\n")
然後我們可以使用replicateConcurrently
從async
包來運行它,同時100次,fmap (all id)
對結果採取一切結果的布爾和。
-- | Run 'testOne' 100 times concurrently, return whether all tests succeeded.
testMany :: IO Bool
testMany =
all id <$> replicateConcurrently 100 testOne