Parallel IO causes random text output in terminal

I use

import Control.Concurrent.ParallelIO.Global main = parallel_ (map processI [1..(sdNumber runParameters)]) >> stopGlobalPool 

Where

 processI :: Int -> IO () 

- some function that reads data from a file, processes it and writes to another file. There is no output to the terminal. The problem is that when starting the program with +RTS -N8 terminal is filled with random text, for example

 piptufuht teata thtsieieo ocnsno e nscsdeoe qnqvuduee ernvnstetiirioasanlil lolwynya. .s w ass uY Ysosopuuue's'nvpvdeeee n dpdp rerdodoub beada bub lel y 

What's happening? Without + RTS there is no mess. I could not reproduce the behavior with a simpler (suitable for publication here) program.

GHC 7.0.3 if it matters

+4
source share
1 answer

Buffering probably prevents the construction of a simple test case. I was able to play it with this (only when starting with + RTS -Nsomething):

 import Control.Concurrent import System.IO main :: IO () main = do hSetBuffering stdout NoBuffering forkIO $ putStrLn "foo" forkIO $ putStrLn "bar" forkIO $ putStrLn "baz" threadDelay 1000 -- Allow things to print 

As Thomas mentioned, you might have to sort this out somehow, although I'm not sure how this can change the writing to files. Here is a simple example of how you can arrange this with Chan . I am sure there is a better way to do this, this is just an example of how I got this so as not to distort the output.

 import Control.Concurrent import Control.Concurrent.Chan import System.IO main :: IO () main = do hSetBuffering stdout NoBuffering ch <- newChan -- Things written here are picked up by stuffWriter forkIO $ stuffWriter ch -- Fire up concurrent stuffWriter forkIO $ writeChan ch "foo" forkIO $ writeChan ch "bar" forkIO $ writeChan ch "baz" threadDelay 1000 -- Allow things to print -- | Write all the things! stuffWriter :: Chan String -> IO () stuffWriter ch = do readChan ch >>= putStrLn -- Block, then write once I've got something stuffWriter ch -- loop... looking for more things to write 

Now your records are now somewhere in sync ( stuffWriter writes things one at a time) and you shouldn't have any more distortion.

+6
source

Source: https://habr.com/ru/post/1388180/


All Articles