Created
December 5, 2013 09:47
-
-
Save JakubOboza/7802767 to your computer and use it in GitHub Desktop.
lulz on forkIO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings, RecordWildCards #-} | |
module Main (main) where | |
import Database.Redis | |
import Control.Concurrent | |
import Control.Concurrent.Chan | |
import Data.ByteString.Char8 as B | |
import Control.Monad.IO.Class | |
import Control.Monad | |
data Stat = Stat { name :: String, num :: Int } deriving (Show) | |
counterBumper' chan = do | |
conn <- Database.Redis.connect defaultConnectInfo | |
forkIO $ forever $ do | |
stat <- readChan chan | |
runRedis conn $ do | |
set ( B.pack $ name stat) (B.pack $ show $ num stat) | |
counterPrinter' chan resChan = do | |
conn <- Database.Redis.connect defaultConnectInfo | |
forkIO $ forever $ do | |
name <- readChan chan | |
runRedis conn $ do | |
response <- get name | |
liftIO $ do writeChan resChan response | |
showResponces chan = do | |
re <- readChan chan | |
case re of | |
Left x -> print x -- Prelude.putStrLn "error" | |
Right y -> case y of | |
Just z -> Prelude.putStrLn $ B.unpack z | |
Nothing -> Prelude.putStrLn "lulz" | |
main :: IO () | |
main = do | |
bumpChan <- newChan | |
printChan <- newChan | |
backChan <- newChan | |
-- start workers | |
counterBumper' bumpChan | |
counterPrinter' printChan backChan | |
writeChan bumpChan $ Stat "kuba" 69 | |
writeChan bumpChan $ Stat "kuba2" 3 | |
writeChan bumpChan $ Stat "kuba3" 78 | |
writeChan printChan "kuba" | |
writeChan printChan "kuba4444" | |
writeChan printChan "kuba2" | |
showResponces backChan | |
showResponces backChan | |
showResponces backChan | |
Prelude.putStrLn "End of Story! Hit enter!" | |
Prelude.getLine | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment