streamly/test/parallel-loops.hs
Harendra Kumar 9c85811905 rename "once" to "yieldM"
and some other related refactoring changes.
2018-06-23 06:50:59 +05:30

27 lines
821 B
Haskell

import Control.Concurrent (myThreadId, threadDelay)
import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
import System.Random (randomIO)
import Streamly
import qualified Streamly.Prelude as S
main = do
hSetBuffering stdout LineBuffering
runStream $ do
x <- S.take 10 $ loop "A" `parallel` loop "B"
S.yieldM $ myThreadId >>= putStr . show
>> putStr " got "
>> print x
where
-- we can just use
-- parallely $ cycle1 $ yieldM (...)
loop :: String -> Serial (String, Int)
loop name = do
S.yieldM $ threadDelay 1000000
rnd <- S.yieldM (randomIO :: IO Int)
S.yieldM $ myThreadId >>= putStr . show
>> putStr " yielding "
>> print rnd
return (name, rnd) `parallel` loop name