concurrent-output-1.10.21: Ungarble output from several threads or commands

Copyright | 2015 Joey Hess <id@joeyh.name>
License | BSD-2-clause
Safe Haskell | None
Language | Haskell2010

System.Console.Concurrent

Contents

- Concurrent output
- Low level access to the output buffer

Description

Concurrent output handling.

     import Control.Concurrent.Async
     import System.Console.Concurrent

     main = withConcurrentOutput $ do
     	outputConcurrent "washed the car\n"
     		`concurrently`
    	outputConcurrent "walked the dog\n"
    		`concurrently`
     	createProcessConcurrent (proc "ls" [])

Synopsis

- withConcurrentOutput :: (MonadIO m, MonadMask m) => m a -> m a
- class Outputable v where
  - toOutput :: v -> Text
- outputConcurrent :: Outputable v => v -> IO ()
- errorConcurrent :: Outputable v => v -> IO ()
- createProcessConcurrent :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
- createProcessForeground :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
- flushConcurrentOutput :: IO ()
- lockOutput :: (MonadIO m, MonadMask m) => m a -> m a
- type ConcurrentProcessHandle = ProcessHandle
- waitForProcessConcurrent :: ConcurrentProcessHandle -> IO ExitCode
- data OutputBuffer
- data StdHandle
  - = StdOut
  - | StdErr
- bufferOutputSTM :: Outputable v => StdHandle -> v -> STM ()
- outputBufferWaiterSTM :: (OutputBuffer -> (OutputBuffer, OutputBuffer)) -> STM (StdHandle, OutputBuffer)
- waitAnyBuffer :: OutputBuffer -> (OutputBuffer, OutputBuffer)
- waitCompleteLines :: OutputBuffer -> (OutputBuffer, OutputBuffer)
- emitOutputBuffer :: StdHandle -> OutputBuffer -> IO ()

# Concurrent output

withConcurrentOutput :: (MonadIO m, MonadMask m) => m a -> m a Source #

Use this around any actions that use `outputConcurrent` or `createProcessConcurrent`, unless `displayConsoleRegions` is being used.

This is necessary to ensure that buffered concurrent output actually gets displayed before the program exits.

class Outputable v where Source #

Values that can be output.

Methods

toOutput :: v -> Text Source #

#### Instances

Instances details

Outputable Text Source # |
Instance details Defined in System.Console.Concurrent.Internal Methods toOutput :: Text -> Text Source #
Outputable Text Source # | Note that using a lazy Text as an Outputable value will buffer it all in memory.
Instance details Defined in System.Console.Concurrent.Internal Methods toOutput :: Text -> Text Source #
Outputable String Source # |
Instance details Defined in System.Console.Concurrent.Internal Methods toOutput :: String -> Text Source #

outputConcurrent :: Outputable v => v -> IO () Source #

Displays a value to stdout.

Uses locking to ensure that the whole output occurs atomically even when other threads are concurrently generating output.

No newline is appended to the value, so if you want a newline, be sure to include it yourself.

When something else is writing to the console at the same time, this does not block. It buffers the value, so it will be displayed once the other writer is done.

When outputConcurrent is used within a call to `displayConsoleRegions`, the output is displayed above the currently open console regions. Only lines ending in a newline are displayed in this case (it uses `waitCompleteLines`).

errorConcurrent :: Outputable v => v -> IO () Source #

Like `outputConcurrent`, but displays to stderr.

(Does not throw an exception.)

createProcessConcurrent :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) Source #

Wrapper around `createProcess` that prevents multiple processes that are running concurrently from writing to stdout/stderr at the same time.

If the process does not output to stdout or stderr, it's run by createProcess entirely as usual. Only processes that can generate output are handled specially:

A process is allowed to write to stdout and stderr in the usual way, assuming it can successfully take the output lock.

When the output lock is held (ie, by another concurrent process, or because `outputConcurrent` is being called at the same time), the process is instead run with its stdout and stderr redirected to a buffer. The buffered output will be displayed as soon as the output lock becomes free.

Note that the the process is waited for by a background thread, so unlike createProcess, neglecting to call waitForProcess will not result in zombie processess.

createProcessForeground :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) Source #

Wrapper around `createProcess` that makes sure a process is run in the foreground, with direct access to stdout and stderr. Useful when eg, running an interactive process.

Note that the the process is waited for by a background thread, so unlike createProcess, neglecting to call waitForProcess will not result in zombie processess.

flushConcurrentOutput :: IO () Source #

Blocks until any processes started by `createProcessConcurrent` have finished, and any buffered output is displayed. Also blocks while `lockOutput` is is use.

`withConcurrentOutput` calls this at the end, so you do not normally need to use this.

lockOutput :: (MonadIO m, MonadMask m) => m a -> m a Source #

Holds a lock while performing an action. This allows the action to perform its own output to the console, without using functions from this module.

While this is running, other threads that try to lockOutput will block. Any calls to `outputConcurrent` and `createProcessConcurrent` will not block, but the output will be buffered and displayed only once the action is done.

type ConcurrentProcessHandle = ProcessHandle Source #

This alias is provided to avoid breaking backwards compatibility.

waitForProcessConcurrent :: ConcurrentProcessHandle -> IO ExitCode Source #

Same as `waitForProcess`; provided to avoid breaking backwards compatibility.

# Low level access to the output buffer

data OutputBuffer Source #

Buffered output.

#### Instances

Instances details

Eq OutputBuffer Source # |
Instance details Defined in System.Console.Concurrent.Internal Methods (==) :: OutputBuffer -> OutputBuffer -> Bool Source # (/=) :: OutputBuffer -> OutputBuffer -> Bool Source #

data StdHandle Source #

Constructors

StdOut |
StdErr |

bufferOutputSTM :: Outputable v => StdHandle -> v -> STM () Source #

Adds a value to the output buffer for later display.

Note that buffering large quantities of data this way will keep it resident in memory until it can be displayed. While `outputConcurrent` uses temp files if the buffer gets too big, this STM function cannot do so.

outputBufferWaiterSTM :: (OutputBuffer -> (OutputBuffer, OutputBuffer)) -> STM (StdHandle, OutputBuffer) Source #

A STM action that waits for some buffered output to become available, and returns it.

The function can select a subset of output when only some is desired; the fst part is returned and the snd is left in the buffer.

This will prevent it from being displayed in the usual way, so you'll need to use `emitOutputBuffer` to display it yourself.

waitAnyBuffer :: OutputBuffer -> (OutputBuffer, OutputBuffer) Source #

waitCompleteLines :: OutputBuffer -> (OutputBuffer, OutputBuffer) Source #

Use with `outputBufferWaiterSTM` to make it only return buffered output that ends with a newline. Anything buffered without a newline is left in the buffer.

emitOutputBuffer :: StdHandle -> OutputBuffer -> IO () Source #

Emits the content of the OutputBuffer to the Handle

If you use this, you should use `lockOutput` to ensure you're the only thread writing to the console.