Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Asynchronicity implementation using MVar
s and free monads.
Synopsis
- newtype FreeAsyncT m a = FreeAsyncT {
- getFreeAsyncT :: ProgramT MVar m a
- type FreeAsync = FreeAsyncT IO
- freeAsync :: MonadIO m => IO a -> FreeAsyncT m a
- asyncMVar :: MVar a -> FreeAsyncT m a
- runFreeAsync :: FreeAsync a -> IO a
- runFreeAsyncT :: MonadIO m => FreeAsyncT m a -> m a
- newtype ConcurrentlyT m a = ConcurrentlyT {
- getConcurrentlyT :: FreeAsyncT m a
- type Concurrently = ConcurrentlyT IO
- concurrently :: MonadIO m => IO a -> ConcurrentlyT m a
- concurrentlyMVar :: MVar a -> ConcurrentlyT m a
- lift' :: Monad m => m a -> ConcurrentlyT m a
- runConcurrentlyT :: MonadIO m => ConcurrentlyT m a -> m a
- runConcurrently :: Concurrently a -> IO a
FreeAsyncT
newtype FreeAsyncT m a Source #
An IO
-like monad with the capability of async/await-style futures.
Synchronous (blocking) computations in this monad can be created using lift
and liftIO
.
Asynchronous computations that can run in the background are created with freeAsync
or asyncMVar
.
To leverage the asynchronicity, you can schedule computations with MonadSchedule
operations such as schedule
or race
.
Caution: Composing computations with Applicative
or Monad
operations like <*>
, >>=
and do
-notation
will force all but the final computation in order:
When running a
, *>
b *>
cb
will not be started before a
has completed.
To start all operations and run them concurrently, use e.g. scheduleWith
.
To use an Applicative
interface for concurrency, have a look at ConcurrentlyT
.
FreeAsyncT | |
|
Instances
type FreeAsync = FreeAsyncT IO Source #
freeAsync :: MonadIO m => IO a -> FreeAsyncT m a Source #
Run an IO
computation in the background.
This returns a "promise", or "future", which completes the computation when run.
asyncMVar :: MVar a -> FreeAsyncT m a Source #
runFreeAsync :: FreeAsync a -> IO a Source #
Like runFreeAsyncT
, but specialized to IO
.
runFreeAsyncT :: MonadIO m => FreeAsyncT m a -> m a Source #
Complete all computations and remove the FreeAsyncT
layer.
Concurrent Applicative
interface
newtype ConcurrentlyT m a Source #
Like FreeAsyncT
, but leverages concurrency in the Applicative
interface.
The central difference to FreeAsyncT
is the Applicative
instance:
concurrently a *> concurrently b *> concurrently c
will launch all three actions immediately
and return when all actions have completed.
On the other hand, concurrently a >>= f
has to compute sequentially.
For more readable syntax, it can be useful to switch the ApplicativeDo
extension on.
The downside of this Applicative
instance is that ConcurrentlyT
can't be an instance of MonadTrans
.
As a drop-in replacement, the function lift'
is supplied.
Caution: To lift an IO
action concurrently, you need to use concurrently
and not liftIO
.
Instances
type Concurrently = ConcurrentlyT IO Source #
concurrently :: MonadIO m => IO a -> ConcurrentlyT m a Source #
concurrentlyMVar :: MVar a -> ConcurrentlyT m a Source #
Like asyncMVar
lift' :: Monad m => m a -> ConcurrentlyT m a Source #
Lift a computation to ConcurrentlyT
.
This replaces the missing MonadTrans
instance.
Caution: Computations lifted with this function cannot be scheduled concurrently!
If this is your intention, concurrently
needs to be used instead.
runConcurrentlyT :: MonadIO m => ConcurrentlyT m a -> m a Source #
Run a ConcurrentlyT
computation to completion, removing the newtype layers.
runConcurrently :: Concurrently a -> IO a Source #
Like runConcurrently
, but specialised to IO
.