Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive Process Communication in Haskell

Tags:

haskell

ipc

I am trying to write a Haskell program that executes an interactive program (also written in Haskell), sending and receiving lines of text. The interactive program reads from stdIn and writes to stdOut using the standard Haskell library.

However, this proved to be more complicated than I anticipated, probably due to Haskell lazyness or some other misterious thing happening on the background. The program apparently goes into deadlock, and does not receive back the line of text it was expected to receive. It does receive the text fine if the interactive program terminates as a result of sending the line of text, but I need the program to keep running and receive more data (it's called interactive for a reason). Sometimes it prints the expected output only after I kill the program receiving the message. The code looks like this:

main = do
    (hin,hout,herr,pl) <- (runInteractiveCommand "./PlayerMain")
    hSetBinaryMode hin False
    hSetBinaryMode hout False
    hSetBuffering hin LineBuffering
    hSetBuffering hout NoBuffering
    hPutStr hin "start\n"
    out <- hGetLine hout
    putStrLn out

I already tried replacing lazy strings with strict Data.Text, but the behaviour was the same. Any light?

like image 728
svelten Avatar asked Aug 20 '26 06:08

svelten


1 Answers

Maybe the reason is that the output of ./PlayerMain is being buffered. ./PlayerMain writes something that doesn't get flushed right away, and then hangs because it keeps expecting more input.

That would explain why you do get the message when the program terminates.

So maybe it will work if you modify ./PlayerMain and set its stdout to NoBuffering.

like image 148
danidiaz Avatar answered Aug 21 '26 20:08

danidiaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!