我目前有以下代码:
main :: IO ()
main = do
(_, Just so, _, _) <- createProcess (proc "ls" ["."]) { std_out = CreatePipe }
_ <- createProcess (proc "sort" []) { std_in = so }
print "foo"
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Couldn't match expected type ‘StdStream’
with actual type ‘GHC.IO.Handle.Types.Handle’
In the ‘std_in’ field of a record
In the first argument of ‘createProcess’, namely
‘(proc "sort" []) {std_in = so}’
In a stmt of a 'do' block:
_ <- createProcess ((proc "sort" []) {std_in = so})
Run Code Online (Sandbox Code Playgroud)
我正在尝试将ls进程的输出通过管道传递到sort进程,但是CreatePipe返回一个Handle对,并且std_in需要一个StdStream。
我将如何将句柄转换为stdstream。
谢谢!
StdStream有一个UseHandle将执行转换的构造函数,因此将代码调整为:
_ <- createProcess (proc "sort" []) { std_in = UseHandle so }
Run Code Online (Sandbox Code Playgroud)
它将运行,并打印排序后的目录列表。
但是,如果要在进程完成后打印“ foo”,则需要先等待两个进程。(无论如何,您都想这样做,否则您将有很多“僵尸”进程徘徊,直到Haskell终止。)将代码调整为:
main = do
(_, Just so, _, ph1) <- createProcess (proc "ls" ["."])
{ std_out = CreatePipe }
(_, _, _, ph2) <- createProcess (proc "sort" []) { std_in = UseHandle so }
waitForProcess ph1
waitForProcess ph2
print "foo"
Run Code Online (Sandbox Code Playgroud)
而且你应该很好走