我想创建一个进程并将我的haskell程序中的一些文本定期写入进程的stdin(来自IO操作)。
以下内容在 GHCi 中正常工作,但在构建和运行时无法正常工作。在 GHCi 中,一切都运行良好,并且定期输入 IO 操作的值。但是,当构建并运行时,在写入进程的标准输入时,它似乎会暂停任意长时间。
我已经使用CreateProcess
(from System.Process
) 创建句柄并尝试过hPutStrLn
(缓冲区设置为NoBuffering
--LineBuffering
也不起作用)。
所以我正在尝试这个process-streaming
包,pipes
但似乎根本没有任何工作。
真正的问题是:我如何从 haskell 创建一个进程并定期写入它?
展示此行为的最小示例:
import System.Process
import Data.IORef
import qualified Data.Text as T -- from the text package
import qualified Data.Text.IO as TIO
import Control.Concurrent.Timer -- from the timers package
import Control.Concurrent.Suspend -- from the suspend package
main = do
(Just hin, _,_,_) <- createProcess_ "bgProcess" $
(System.Process.proc "grep" ["10"]) { std_in = CreatePipe }
ref <- newIORef 0 :: IO (IORef Int)
flip repeatedTimer (msDelay 1000) $ do
x <- atomicModifyIORef' ref $ \x -> (x + 1, x)
hSetBuffering hin NoBuffering
TIO.hPutStrLn hin $ T.pack $ show x
任何帮助将不胜感激。