Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Communicating with a forked I/O action


A quick and easy way to launch an I/O type function in the background is by calling the forkIO function provided by the Control.Concurrent package. In this recipe, we will be communicating with forked I/O actions by sending messages using a variable type called MVar.

Getting ready

Install the HTTP package from cabal as follows:

$ cabal install HTTP

How to do it…

  1. Import the relevant packages as follows:

    import Network.HTTP
    import Control.Concurrent
  2. Create a new variable that will be used by the fork process. The newEmptyMVar function is of the IO (MVar a) type, so we will extract the expression out and label it m as follows:

    main = do
      m <- newEmptyMVar
      forkIO $ process m
  3. After running the fork, send it some data by calling putMVar :: MVar a -> a -> IO (), as shown in the following lines of code. The variable will hold the given value, and the forked process waiting on that data will resume:

      putStrLn "sending first website..."
      putMVar m "http...