HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleprocess-1.6.26.1Haskell2010

System.Process.CommunicationHandle

  • 1 type
  • 6 values
  • Packageprocess-1.6.26.1
  • Exports7
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInternal.hs

CommunicationHandle: a Handle that can be serialised,

4 declarations
newtypenewtype CommunicationHandle
#

Gets a GHC Handle File description from the given OS Handle or POSIX fd.

A CommunicationHandle is an abstraction over operating-system specific internal representation of a Handle, which can be communicated through a command-line interface.

In a typical use case, the parent process creates a pipe, using e.g. createWeReadTheyWritePipe or createTheyReadWeWritePipe.

  • One end of the pipe is a Handle, which can be read from/written to by the parent process.

  • The other end is a CommunicationHandle, which can be inherited by a child process. A reference to the handle can be serialised (using the Show instance), and passed to the child process. It is recommended to close the parent's reference to the CommunicationHandle using closeCommunicationHandle after it has been inherited by the child process.

  • The child process can deserialise the CommunicationHandle (using the Read instance), and then use openCommunicationHandleWrite or openCommunicationHandleRead in order to retrieve a Handle which it can write to/read from.

readCreateProcessWithExitCodeCommunicationHandle provides a high-level API to this functionality. See there for example code.

Instances4Eq, Ord, Read, Show

Creating CommunicationHandles to communicate with

2 declarations

High-level API

1 declaration
valuereadCreateProcessWithExitCodeCommunicationHandle
  1. :: NFData a
  2. => ((CommunicationHandle, CommunicationHandle) -> CreateProcess)

    Process to spawn, given a (read, write) pair of CommunicationHandles that are inherited by the spawned process

  3. -> (Handle -> IO a)

    read action

  4. -> (Handle -> IO ())

    write action

  5. -> IO (ExitCode, a)
#

A version of readCreateProcessWithExitCode that communicates with the child process through a pair of CommunicationHandles.

Example usage:

readCreateProcessWithExitCodeCommunicationHandle
  (\(chTheyRead, chTheyWrite) -> proc "child-exe" [show chTheyRead, show chTheyWrite])
  (\ hWeRead -> hGetContents hWeRead)
  (\ hWeWrite -> hPut hWeWrite "xyz")

where child-exe is a separate executable that is implemented as:

main = do
  [chRead, chWrite] <- getArgs
  hRead  <- openCommunicationHandleRead  $ read chRead
  hWrite <- openCommunicationHandleWrite $ read chWrite
  input <- hGetContents hRead
  hPut hWrite $ someFn input
  hClose hWrite