3Streamly is a library to make concurrent programming a joy. The venerable
4`async` package is the go to package for concurrent programming for most
5Haskellers. Streamly is a higher level library than `async` and provides a lot
6more power and functionality, using a simpler and concise expression of
7concurrency. At a high level, you should be able to express everything with
8streamly that you can with `async`, if you can't please raise an issue. If you
9are familiar with `async`, in this document we highlight how streamly can be
10used where you would use `async`.
12## `async/wait` vs Concurrent Streams
14Unlike `async`, streamly does not use a spawn and `wait` model. Streamly uses
15a more high level approach to concurrency and has no explicit notion of
16threads. In streamly, we compose multiple actions as a stream and then express
17whether you want to run the actions in the stream `serially` or `parallely`.
18There are many different ways in which you can run streams concurrently, see
19the reference documentation for details.
21Since there is no explicit notion of threads in streamly, there are no
22equivalents of `async`, `wait`, `cancel`, `poll` or `link` combinators from the
25Since streamly is a monad transformer it can work with all monads and not just
26IO, you won't need adaptations like `lifted-async` to use it for a generic
29## Using Streamly for Concurrency
31You can write all of your program in a streamly monad and use the full power of
32the library. Streamly can be used as a direct replacement of the IO monad with
33no loss of performance, and no change in code except using `liftIO` or `yieldM`
34to run any IO actions. Streamly IO monads (e.g. `SerialT IO`) are just a
35generalization of the IO monad with non-deterministic composition of streams
38However, if you would like to just run only some concurrent portions of your
39program using streamly, you can do that too. Just use `runStream` if you want
40to run the stream without collecting the outputs of the concurrent actions or
41use `toList` if you want to convert the output stream into a list. Other
42stream folding operations can also be used, see the docs for more details.
44## Features as Compared with `async`
46Use the following imports to run the snippets shown below:
50import Streamly.Prelude ((|:))
51import qualified Streamly.Prelude as S
52import qualified Data.Text as Text
53import Control.Concurrent (threadDelay)
56Let us simulate a URL fetch with a delay of `n` seconds using the following
60getURL :: Int -> IO String
61getURL n = threadDelay (n * 1000000) >> return (show n)
63getURLText n = getURL n >>= return . Text.pack
68You can run any number of actions concurrently. For example, to fetch two URLs
72 urls <- S.toList $ parallely $ getURL 2 |: getURL 1 |: S.nil
75This would return the results in their arrival order i.e. first 1 and then 2.
76If you want to preserve the order of the results, use the lookahead style
77stream `aheadly` instead. In the following example both URLs are fetched
78concurrently, and even though URL 1 arrives before URL 2 the results will
79return 2 first and then 1.
82 urls <- S.toList $ aheadly $ getURL 2 |: getURL 1 |: S.nil
87Use `runStream` instead of `toList` to run the actions but ignore the results:
90 runStream $ parallely $ getURL 1 |: getURL 2 |: S.nil
93### Concurrent Applicative
95If the actions that you are executing result in different output types you can
96use applicative zip to collect the results or to directly apply them to a
100 tuples <- S.toList $ zipAsyncly $
101 (,) <$> S.yieldM (getURLString 1) <*> S.yieldM (getURLText 2)
106To run multiple actions and use the first result:
109 urls <- S.toList $ S.take 1 $ parallely $ getURL 1 |: getURL 2 |: S.nil
112After the first result arrives, the rest of the actions are canceled
113automatically. You can take first `n` results as they arrive:
116 urls <- S.toList $ S.take 2 $ parallely $ getURL 1 |: getURL 2 |: S.nil
121There are many ways to map concurrently on a container and collect the results:
123You can create a concurrent stream from a `Foldable` container of monadic
127 urls <- S.toList $ aheadly $ S.fromFoldableM $ fmap getURL [1..3]
130You can first convert a `Foldable` into a stream and then map an action on the
134 urls <- S.toList $ aheadly $ S.mapM getURL $ foldMap return [1..3]
137You can map a monadic action to a `Foldable` container to convert it into a
138stream and at the same time fold it:
141 urls <- S.toList $ aheadly $ foldMap (S.yieldM . getURL) [1..3]
144### replicateConcurrently
146Streamly has not just the equivalent of `replicateConcurrently` which is
147`replicateM` but many more ways to generate concurrent streams, for example,
148`|:`, `unfoldrM`, `repeatM`, `iterateM`, `fromFoldableM` etc. See the
149[Streamly.Prelude](https://hackage.haskell.org/package/streamly/docs/Streamly-Prelude.html)
150module documentation for more details.
153 xs <- S.toList $ parallely $ S.replicateM 2 $ getURL 1
158The stream resulting from concurrent actions can be mapped serially or
161To map serially just use `fmap`:
164 xs <- S.toList $ parallely $ fmap (+1) $ return 1 |: return 2 |: S.nil
167To map a monadic action concurrently on all elements of the stream use `mapM`:
170 xs <- S.toList $ parallely $ S.mapM (\x -> return (x + 1))
171 $ return 1 |: return 2 |: S.nil
176The `Semigroup` instances of streamly merge multiple streams serially or
181The `Monad` instances of streamly nest loops concurrently (concurrent
186Streamly has very little concurrency overhead (ranging from a few 100
187nanoseconds to a few microseconds on a 2.2 GHz Intel Core i7), you can even run
188very lightweight actions in parallel without worrying about the overhead of
189concurrency. See the performance benchmarks [comparing streamly with the `async`
190package in this repo](https://github.com/composewell/concurrency-benchmarks).
194There is much more that you can do with streamly. For example, you can use the
195`maxThreads` combinator to restrict the total number of concurrent threads or
196use the `maxBuffer` combinator to restrict the total number of bufferred
197results or you can use the `avgRate` combinator to control the rate at which
198the concurrent actions are executed.
200See the [haddock documentation on
201hackage](https://hackage.haskell.org/package/streamly) and [a comprehensive tutorial
202here](https://hackage.haskell.org/package/streamly/docs/Streamly-Tutorial.html).
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list