- Packageunliftio-0.2.25.1
- Exports67
- LanguageHaskell2010
- LicenceMIT
- SourceAsync.hs
Asynchronous actions
1 declarationSpawning
Unlifted async.
Unlifted asyncBound.
Unlifted asyncOn.
Unlifted asyncWithUnmask.
Unlifted asyncOnWithUnmask.
Spawning with automatic cancelation
Unlifted withAsync.
Unlifted withAsyncBound.
Unlifted withAsyncOn.
Unlifted withAsyncWithUnmask.
Unlifted A.withAsyncOnWithMask.
Querying Asyncs
Lifted wait.
Lifted poll.
Lifted waitCatch.
Lifted cancel.
Lifted uninterruptibleCancel.
Lifted cancelWith. Additionally uses toAsyncException to ensure async exception safety.
STM operations
A version of wait that can be used inside an STM transaction.
A version of poll that can be used inside an STM transaction.
A version of waitCatch that can be used inside an STM transaction.
Waiting for multiple Asyncs
Lifted waitAny.
Lifted waitAnyCatch.
Lifted waitAnyCancel.
Lifted waitAnyCatchCancel.
Lifted waitEither.
Lifted waitEitherCatch.
Lifted waitEitherCancel.
Lifted waitEitherCatchCancel.
Lifted waitEither_.
Lifted waitBoth.
Waiting for multiple Asyncs in STM
A version of waitAny that can be used inside an STM transaction.
A version of waitAnyCatch that can be used inside an STM transaction.
A version of waitEither that can be used inside an STM transaction.
A version of waitEitherCatch that can be used inside an STM transaction.
A version of waitEither_ that can be used inside an STM transaction.
A version of waitBoth that can be used inside an STM transaction.
Linking
Lifted link.
Lifted link2.
Pooled concurrency
pooledMapConcurrentlyN :: (MonadUnliftIO m, Traversable t)=> IntMax. number of threads. Should not be less than 1.
-> (a -> m b)-> t a-> m (t b)
Like mapConcurrently from async, but instead of one thread per element, it does pooling from a set of threads. This is useful in scenarios where resource consumption is bounded and for use cases where too many concurrent tasks aren't allowed.
Example usage
import Say
action :: Int -> IO Int
action n = do
tid <- myThreadId
sayString $ show tid
threadDelay (2 * 10^6) -- 2 seconds
return n
main :: IO ()
main = do
yx <- pooledMapConcurrentlyN 5 (\x -> action x) [1..5]
print yx
On executing you can see that five threads have been spawned:
$ ./pool
ThreadId 36
ThreadId 38
ThreadId 40
ThreadId 42
ThreadId 44
[1,2,3,4,5]
Let's modify the above program such that there are less threads than the number of items in the list:
import Say
action :: Int -> IO Int
action n = do
tid <- myThreadId
sayString $ show tid
threadDelay (2 * 10^6) -- 2 seconds
return n
main :: IO ()
main = do
yx <- pooledMapConcurrentlyN 3 (\x -> action x) [1..5]
print yx
On executing you can see that only three threads are active totally:
$ ./pool
ThreadId 35
ThreadId 37
ThreadId 39
ThreadId 35
ThreadId 39
[1,2,3,4,5]
Similar to pooledMapConcurrentlyN but with number of threads set from getNumCapabilities. Usually this is useful for CPU bound tasks.
pooledMapConcurrentlyN_ :: (MonadUnliftIO m, Foldable f)=> IntMax. number of threads. Should not be less than 1.
-> (a -> m b)-> f a-> m ()
Like pooledMapConcurrentlyN but with the return value discarded.
Like pooledMapConcurrently but with the return value discarded.
pooledForConcurrentlyN :: (MonadUnliftIO m, Traversable t)=> IntMax. number of threads. Should not be less than 1.
-> t a-> (a -> m b)-> m (t b)
Similar to pooledMapConcurrentlyN but with flipped arguments.
Similar to pooledForConcurrentlyN but with number of threads set from getNumCapabilities. Usually this is useful for CPU bound tasks.
pooledForConcurrentlyN_ :: (MonadUnliftIO m, Foldable t)=> IntMax. number of threads. Should not be less than 1.
-> t a-> (a -> m b)-> m ()
Like pooledMapConcurrentlyN_ but with flipped arguments.
Like pooledMapConcurrently_ but with flipped arguments.
pooledReplicateConcurrentlyN :: MonadUnliftIO m=> IntMax. number of threads. Should not be less than 1.
-> IntNumber of times to perform the action.
-> m a-> m [a]
Pooled version of replicateConcurrently. Performs the action in the pooled threads.
pooledReplicateConcurrently :: MonadUnliftIO m=> IntNumber of times to perform the action.
-> m a-> m [a]
Similar to pooledReplicateConcurrentlyN but with number of threads set from getNumCapabilities. Usually this is useful for CPU bound tasks.
pooledReplicateConcurrentlyN_ :: MonadUnliftIO m=> IntMax. number of threads. Should not be less than 1.
-> IntNumber of times to perform the action.
-> m a-> m ()
Pooled version of replicateConcurrently_. Performs the action in the pooled threads.
pooledReplicateConcurrently_ :: MonadUnliftIO m=> IntNumber of times to perform the action.
-> m a-> m ()
Similar to pooledReplicateConcurrently_ but with number of threads set from getNumCapabilities. Usually this is useful for CPU bound tasks.
Convenient utilities
15 declarationsUnlifted race.
Unlifted race_.
Unlifted concurrently.
Unlifted concurrently_.
Executes a Traversable container of items concurrently, it uses the Flat type internally.
Similar to mapConcurrently but with arguments flipped
Executes a Traversable container of items concurrently, it uses the Flat type internally. This function ignores the results.
Similar to mapConcurrently_ but with arguments flipped
Unlifted replicateConcurrently.
Unlifted replicateConcurrently_.
Unlifted Concurrently.
Constructors
ConcurrentlyrunConcurrently :: m a
Instances5Functor, Applicative, Alternative, Semigroup, Monoid
Monad m => Functor (Concurrently m)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncMonadUnliftIO m => Applicative (Concurrently m)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncMonadUnliftIO m => Alternative (Concurrently m)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncComposing two unlifted Concurrently values using Alternative is the equivalent to using a race combinator, the asynchrounous sub-routine that returns a value first is the one that gets it's value returned, the slowest sub-routine gets cancelled and it's thread is killed.
(MonadUnliftIO m, Semigroup a) => Semigroup (Concurrently m a)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncOnly defined by
asyncforbase >= 4.9.(Semigroup a, Monoid a, MonadUnliftIO m) => Monoid (Concurrently m a)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.Async
A more efficient alternative to Concurrently, which reduces the
number of threads that need to be forked. For more information, see
this blog post.
This is provided as a separate type to Concurrently as it has a slightly different API.
Use the conc function to construct values of type Conc, and
runConc to execute the composed actions. You can use the
Applicative instance to run different actions and wait for all of
them to complete, or the Alternative instance to wait for the
first thread to complete.
In the event of a runtime exception thrown by any of the children threads, or an asynchronous exception received in the parent thread, all threads will be killed with an AsyncCancelled exception and the original exception rethrown. If multiple exceptions are generated by different threads, there are no guarantees on which exception will end up getting rethrown.
For many common use cases, you may prefer using helper functions in this module like mapConcurrently.
There are some intentional differences in behavior to
Concurrently:
Children threads are always launched in an unmasked state, not the inherited state of the parent thread.
Note that it is a programmer error to use the Alternative
instance in such a way that there are no alternatives to an empty,
e.g. runConc (empty | empty). In such a case, a ConcException
will be thrown. If there was an Alternative in the standard
libraries without empty, this library would use it instead.
Instances5Functor, Applicative, Alternative, Semigroup, Monoid
Functor m => Functor (Conc m)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncMonadUnliftIO m => Applicative (Conc m)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncMonadUnliftIO m => Alternative (Conc m)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.Async(MonadUnliftIO m, Semigroup a) => Semigroup (Conc m a)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.Async(Monoid a, MonadUnliftIO m) => Monoid (Conc m a)Defined in unliftio-0.2.25.1 · UnliftIO.Internals.Async
Construct a value of type Conc from an action. Compose these values using the typeclass instances (most commonly Applicative and Alternative) and then run with runConc.
Run a Conc value on multiple threads.
Things that can go wrong in the structure of a Conc. These are programmer errors.
Instances6Eq, Ord, Show, Generic, Exception, Rep
Eq ConcExceptionDefined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncOrd ConcExceptionDefined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncShow ConcExceptionDefined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncGeneric ConcExceptionDefined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncException ConcExceptionDefined in unliftio-0.2.25.1 · UnliftIO.Internals.Asynctype Rep ConcException = D1 ('MetaDataDefined in unliftio-0.2.25.1 · UnliftIO.Internals.Async"ConcException"
"UnliftIO.Internals.Async"
"unliftio-0.2.25.1-4jnE6Lke0TN96UoVpMulRS"
'False) (C1 ('MetaCons"EmptyWithNoAlternative"
'PrefixI 'False) U1)
Re-exports
1 declarationThe exception thrown by cancel to terminate a thread.
Instances3Eq, Show, Exception
Eq AsyncCancelledDefined in async-2.2.5 · Control.Concurrent.Async.InternalShow AsyncCancelledDefined in async-2.2.5 · Control.Concurrent.Async.InternalException AsyncCancelledDefined in async-2.2.5 · Control.Concurrent.Async.Internal