unsafe-promises-0.0.1: Create pure futures using lazy IO.

Safe HaskellSafe-Infered

Control.Concurrent.Promise.Unsafe

Contents

Description

An experimental library for lazy promises that can be evaluated in pure code.

Evaluation of a promise before its thread completes results in an indefinite block. This is accomplished by the use of unsafeInterleaveIO. Thus, care should be taken in using this library, since it couples the execution time of pure code with an arbitrary IO computation. Using System.Timeout from the timeout package can help to ensure that forcing a promise is always well-defined.

For safer implementations of promises, see Control.Concurrent.Spawn from the spawn package, and Control.Concurrent.Future from the future package.

Synopsis

Creating promises

promise :: IO a -> IO aSource

Forks an IO computation as a thread and immediately returns a lazy future. Evaluating the future before the thread completes causes it to wait for a result. If the thread halts with a thrown exception, then evaluating the future will re-throw the exception.

tryPromise :: IO a -> IO (Result a)Source

Like promise, but does not rethrow exceptions. Instead the exception is wrapped as part of the Result.

Creating lists of promises

promises :: [IO a] -> IO [a]Source

Forks a sequence of IO computations in multiple threads, and immediately returns a list of futures. The order of the futures is determined by the order in which the threads terminate. If an exception is thrown by the list of threads, then the exception is re-thrown when its corresponding future is evaluated.

tryPromises :: [IO a] -> IO [Result a]Source

Like promises, but doesn't re-throw exceptions.