[Haskell-beginners] Haskeline and forkIO
Jeff C. Britton
jcb at iteris.com
Tue Aug 26 22:07:14 UTC 2014
I am trying to modify an example in RealWorldHaskell from Chapter 24.
The example is the first code snippet labeled -- file: ch24/Compressor.hs
I am trying to replace the use of Readline with Haskeline.
In my code the forkIO thread does not run.
I guessed that since the result of the worker thread was thrown away that perhaps laziness was the problem.
So, I attempted to use `seq`, but that does not work either.
I am able to run the RealWorldHaskell example.
I am using GHC 7.8.3.
I have tried runhaskell with and without the -threaded option and on both Linux and Windows 7.
import Control.Concurrent (forkIO)
import Control.Exception
import qualified Data.ByteString.Lazy as L
import System.Console.Haskeline hiding (handle)
-- Provided by the 'zlib' package on http://hackage.haskell.org/
import Codec.Compression.GZip (compress)
-- Read the file, compress the data, write the compressed data
worker :: FilePath -> IO ()
worker path = L.readFile path >>= L.writeFile (path ++ ".gz") . compress
-- Run the worker on a new thread
runWorker :: FilePath -> IO()
runWorker path = handle (print :: SomeException -> IO ()) $ do
forkIO (worker path)
return ()
loop :: InputT IO ()
loop = do
maybeLine <- getInputLine "Enter a file to compress> "
case maybeLine of
Nothing -> return () -- user entered EOF
Just "" -> return () -- treat no name as "want to quit"
Just path ->
let f = runWorker path
in
f `seq` do
return f
loop
main = runInputT defaultSettings loop
More information about the Beginners
mailing list