on 266148c help sign in
streamly/test/parallel-loops.hs
26 lines 821 B history raw
1import Control.Concurrent (myThreadId, threadDelay)
2import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
3import System.Random (randomIO)
4import Streamly
5import qualified Streamly.Prelude as S
7main = do
8 hSetBuffering stdout LineBuffering
9 runStream $ do
10 x <- S.take 10 $ loop "A" `parallel` loop "B"
11 S.yieldM $ myThreadId >>= putStr . show
12 >> putStr " got "
13 >> print x
15 where
17 -- we can just use
18 -- parallely $ cycle1 $ yieldM (...)
19 loop :: String -> Serial (String, Int)
20 loop name = do
21 S.yieldM $ threadDelay 1000000
22 rnd <- S.yieldM (randomIO :: IO Int)
23 S.yieldM $ myThreadId >>= putStr . show
24 >> putStr " yielding "
25 >> print rnd
26 return (name, rnd) `parallel` loop name