@@ -11,6 +11,16 @@
11-- Stability : experimental
11-- Stability : experimental
12-- Portability : GHC
12-- Portability : GHC
14+-- To run examples in this module:
16+-- >>> import qualified Streamly.Prelude as Stream
17+-- >>> import Control.Concurrent (threadDelay)
20+-- threadDelay (n * 1000000) -- sleep for n seconds
21+-- putStrLn (show n ++ " sec") -- print "n sec"
15module Streamly.Internal.Data.Stream.Async
25module Streamly.Internal.Data.Stream.Async
@@ -61,6 +71,15 @@ import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
62#include "Instances.hs"
72#include "Instances.hs"
75+-- >>> import Control.Concurrent (threadDelay)
78+-- threadDelay (n * 1000000) -- sleep for n seconds
79+-- putStrLn (show n ++ " sec") -- print "n sec"
64-------------------------------------------------------------------------------
83-------------------------------------------------------------------------------
66-------------------------------------------------------------------------------
85-------------------------------------------------------------------------------
@@ -637,27 +656,33 @@ infixr 6 `async`
637-- | Merges two streams, both the streams may be evaluated concurrently,
656-- | Merges two streams, both the streams may be evaluated concurrently,
638-- outputs from both are used as they arrive:
657-- outputs from both are used as they arrive:
659+-- >>> import Streamly.Prelude (async)
640-- >>> stream1 = Stream.yieldM (delay 4)
660-- >>> stream1 = Stream.yieldM (delay 4)
641-- >>> stream2 = Stream.yieldM (delay 2)
661-- >>> stream2 = Stream.yieldM (delay 2)
642-- >>> Stream.toList $ stream1 `async` stream2
662-- >>> Stream.toList $ stream1 `async` stream2
645-- Multiple streams can be combined. With enough threads, all of them can be
667-- Multiple streams can be combined. With enough threads, all of them can be
646-- scheduled simultaneously:
668-- scheduled simultaneously:
648-- >>> stream3 = Stream.yieldM (delay 1)
670-- >>> stream3 = Stream.yieldM (delay 1)
649-- >>> Stream.toList $ stream1 `async` stream2 `async` stream3
671-- >>> Stream.toList $ stream1 `async` stream2 `async` stream3
650-- [1,2,4]
673-- [1,2,4]
652-- With 2 threads, only two can be scheduled at a time, when one of those
675-- With 2 threads, only two can be scheduled at a time, when one of those
653-- finishes, the third one gets scheduled:
676-- finishes, the third one gets scheduled:
655-- >>> Stream.toList $ Stream.maxThreads 2 $ stream1 `async` stream2 `async` stream3
678-- >>> Stream.toList $ Stream.maxThreads 2 $ stream1 `async` stream2 `async` stream3
656-- [2,1,4]
680-- [2,1,4]
658-- With a single thread, it becomes serial:
682-- With a single thread, it becomes serial:
660-- >>> Stream.toList $ Stream.maxThreads 1 $ stream1 `async` stream2 `async` stream3
684-- >>> Stream.toList $ Stream.maxThreads 1 $ stream1 `async` stream2 `async` stream3
661-- [4,2,1]
686-- [4,2,1]
663-- Only streams are scheduled for async evaluation, how actions within a
688-- Only streams are scheduled for async evaluation, how actions within a
@@ -667,16 +692,18 @@ infixr 6 `async`
667-- In the following example, both the streams are scheduled for concurrent
692-- In the following example, both the streams are scheduled for concurrent
668-- evaluation but each individual stream is evaluated serially:
693-- evaluation but each individual stream is evaluated serially:
670−-- >>> stream1 = Stream.fromListM $ map delay [3,3] -- SerialT IO Int
695+-- >>> stream1 = Stream.fromListM $ Prelude.map delay [3,3] -- SerialT IO Int
671−-- >>> stream2 = Stream.fromListM $ map delay [1,1] -- SerialT IO Int
696+-- >>> stream2 = Stream.fromListM $ Prelude.map delay [1,1] -- SerialT IO Int
672-- >>> Stream.toList $ stream1 `async` stream2 -- IO [Int]
697-- >>> Stream.toList $ stream1 `async` stream2 -- IO [Int]
673-- [1,1,3,3]
699-- [1,1,3,3]
675-- If total threads are 2, the third stream is scheduled only after one of the
701-- If total threads are 2, the third stream is scheduled only after one of the
676-- first two has finished:
702-- first two has finished:
678−-- >>> stream3 = Stream.fromListM $ map delay [2,2] -- SerialT IO Int
704+-- >>> stream3 = Stream.fromListM $ Prelude.map delay [2,2] -- SerialT IO Int
679-- >>> Stream.toList $ Stream.maxThreads 2 $ stream1 `async` stream2 `async` stream3 -- IO [Int]
705-- >>> Stream.toList $ Stream.maxThreads 2 $ stream1 `async` stream2 `async` stream3 -- IO [Int]
680-- [1,1,3,2,3,2]
707-- [1,1,3,2,3,2]
682-- Thus 'async' goes deep in first few streams rather than going wide in all
709-- Thus 'async' goes deep in first few streams rather than going wide in all
@@ -867,6 +894,7 @@ infixr 6 `wAsync`
867-- With a single thread, 'async' starts behaving like 'serial' while 'wAsync'
894-- With a single thread, 'async' starts behaving like 'serial' while 'wAsync'
868-- starts behaving like 'wSerial'.
895-- starts behaving like 'wSerial'.
897+-- >>> import Streamly.Prelude (wAsync)
870-- >>> stream1 = Stream.fromList [1,2,3]
898-- >>> stream1 = Stream.fromList [1,2,3]
871-- >>> stream2 = Stream.fromList [4,5,6]
899-- >>> stream2 = Stream.fromList [4,5,6]
872-- >>> Stream.toList $ Stream.asyncly $ Stream.maxThreads 1 $ stream1 `async` stream2
900-- >>> Stream.toList $ Stream.asyncly $ Stream.maxThreads 1 $ stream1 `async` stream2