Control.Distributed.Fork

Contents

Description

This module provides a common interface for offloading an IO action to remote executors.

It uses StaticPointers language extension and distributed-closure library for serializing closures to run remotely. This blog post is a good introduction for those.

In short, if you you need a Closure a:

• If a is statically known (eg. a top level value, or if it does not depend on anything on the scope), use static keyword coming from StaticPointers extension.
• If a is a runtime value, use cpure to lift it to Closure a. It will ask for a (Closure (Dict (Serializable a))). If there is (Binary a) and (Typeable a) instances, you can just use (static Dict) for that.

One important constraint when using this library is that it assumes the remote environment is capable of executing the exact same binary. On most cases, this requires your host environment to be Linux. In future I plan to provide a set of scripts using Docker to overcome this limitation.

Synopsis

# Documentation

fork :: Backend -> Closure (Dict (Serializable a)) -> Closure (IO a) -> IO (Handle a) Source #

Asynchronously executes the given function using the Backend and returns an Handle.

{-# LANGUAGE StaticPointers #-}

import Control.Distributed.Fork
import Control.Distributed.Fork.LocalProcessBackend

main :: IO ()
main = do
initDistributedFork
handle <- fork localProcessBackend (static Dict) (static (return "Hello World!"))
await handle >>= putStrLn


On distributed-fork, we run the same binary both in your machine (called "driver") and in the remote environment (called "executor"). In order for the program to act according to where it is, you should call this function as the first thing in your main:

main = do
initDistributedFork
...


data Backend Source #

Backend is responsible for running your functions in a remote environment.

See:

# Handle

data Handle a Source #

Result of a fork is an Handle where you can await a result.

await :: Handle a -> IO a Source #

Blocks until the Handle completes.

Can throw ExecutorFailedException.

# Exceptions

Constructors

 ExecutorFailedException Text

Instances

 Source # Methods Source # Methods Source #

# Re-exports

type Serializable a = (Binary a, Typeable * a) #

Values that can be sent across the network.

data Closure a :: * -> * #

Type of serializable closures. Abstractly speaking, a closure is a code reference paired together with an environment. A serializable closure includes a shareable code reference (i.e. a StaticPtr). Closures can be serialized only if all expressions captured in the environment are serializable.

Instances

 Methods Typeable * a => Binary (Closure a) Methodsput :: Closure a -> Put #get :: Get (Closure a) #putList :: [Closure a] -> Put #

cap :: Typeable * a => Closure (a -> b) -> Closure a -> Closure b #

Closure application. Note that Closure is not a functor, let alone an applicative functor, even if it too has a meaningful notion of application.

cpure :: Closure (Dict (Serializable a)) -> a -> Closure a #

A closure can be created from any serializable value. cpure corresponds to Control.Applicative's pure, but restricted to lifting serializable values only.

data Dict a :: Constraint -> * where #

Values of type Dict p capture a dictionary for a constraint of type p.

e.g.

Dict :: Dict (Eq Int)


captures a dictionary that proves we have an:

instance Eq 'Int


Pattern matching on the Dict constructor will bring this instance into scope.

Constructors

 Dict :: Dict a

Instances