taskforce (taskforce v1.3.0)

Copy Markdown View Source

Parallelise arbitrary tasks in a controlled way.

taskforce runs a group of independent tasks concurrently, bounding both the number of workers and how long execution may take, and gathers their results.

Build tasks with task/3, then run them with execute/1 or execute/2. See the README for an overview and examples.

Summary

Functions

Run Tasks concurrently with default execution options.

Run Tasks concurrently, subject to ExecutionOptions.

Create a task that runs Function applied to Args.

Types

execution_options()

-type execution_options() :: #{timeout => pos_integer(), max_workers => pos_integer()}.

result()

-type result() ::
          #{completed := #{TaskId :: term() => TaskResult :: term()},
            individual_timeouts := [TaskId :: term()],
            global_timeouts := [TaskId :: term()]}.

task()

-opaque task()

task_settings()

-type task_settings() :: #{timeout := pos_integer()}.

tasks()

-type tasks() :: #{TaskId :: term() => Task :: task()}.

Functions

execute(Tasks)

-spec execute(Tasks) -> Result when Tasks :: tasks(), Result :: result().

Run Tasks concurrently with default execution options.

Equivalent to execute(Tasks, #{}).

execute(Tasks, ExecutionOptions)

-spec execute(Tasks, ExecutionOptions) -> Result
                 when Tasks :: tasks(), ExecutionOptions :: execution_options(), Result :: result().

Run Tasks concurrently, subject to ExecutionOptions.

ExecutionOptions may bound the global timeout (in milliseconds) and the maximum number of concurrent workers (max_workers); both default sensibly when omitted. Returns the tasks that completed, along with the ids of those that hit an individual_timeouts or a global_timeouts.

task(Function, Args, TaskSettings)

-spec task(Function, Args, TaskSettings) -> Task
              when Function :: fun(), Args :: [term()], TaskSettings :: task_settings(), Task :: task().

Create a task that runs Function applied to Args.

TaskSettings must contain a timeout, in milliseconds, bounding how long the individual task may run before being given up on.