Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Created December 5, 2013 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakubOboza/7802767 to your computer and use it in GitHub Desktop.
Save JakubOboza/7802767 to your computer and use it in GitHub Desktop.
lulz on forkIO
{-# 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