polysemy-conc-0.9.0.0: Polysemy effects for concurrency
Safe HaskellSafe-Inferred
LanguageHaskell2010

Polysemy.Conc.Async

Description

 
Synopsis

Documentation

withAsyncBlock :: Members [Resource, Async] r => Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a Source #

Run the first action asynchronously while the second action executes, then cancel the first action. Passes the handle into the action to allow it to await its result.

When cancelling, this variant will wait indefinitely for the thread to be gone.

withAsyncWait :: TimeUnit u => Members [Resource, Race, Async] r => u -> Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a Source #

Run the first action asynchronously while the second action executes, then cancel the first action. Passes the handle into the action to allow it to await its result.

When cancelling, this variant will wait for the specified interval for the thread to be gone.

withAsync :: Members [Resource, Race, Async] r => Sem r b -> (Async (Maybe b) -> Sem r a) -> Sem r a Source #

Run the first action asynchronously while the second action executes, then cancel the first action. Passes the handle into the action to allow it to await its result.

When cancelling, this variant will wait for 500ms for the thread to be gone.

withAsync_ :: Members [Resource, Race, Async] r => Sem r b -> Sem r a -> Sem r a Source #

Run the first action asynchronously while the second action executes, then cancel the first action. Discards the handle, expecting the async action to either terminate or be cancelled.

When cancelling, this variant will wait for 500ms for the thread to be gone.

scheduleAsync :: forall res b r a. Members [ScopedSync res (), Async, Race] r => Sem r b -> (Async (Maybe b) -> Sem (Sync () ': r) () -> Sem (Sync () ': r) a) -> Sem r a Source #

Run an action with async, but don't start it right away, so the thread handle can be processed before the action executes.

Takes a callback function that is invoked after spawning the thread. The callback receives the Async handle and a unit action that starts the computation.

This is helpful if the Async has to be stored in state and the same state is written when the action finishes. In that case, the race condition causes the handle to be written over the finished state.

makeRequest = put Nothing

main = scheduleAsync makeRequest  handle start -> do
  put (Just handle)
  start -- now makeRequest is executed

scheduleAsyncIO :: forall b r a. Members [Resource, Async, Race, Embed IO] r => Sem r b -> (Async (Maybe b) -> Sem (Sync () ': r) () -> Sem (Sync () ': r) a) -> Sem r a Source #

Variant of scheduleAsync that directly interprets the MVar used for signalling.