Eldritch

The dark arts of concurrent programming.

A DSL that adds parallel programming constructs to make your life a little easier.

Code quality

Build Status Coverage Status Code Climate

Features

async methods

Async methods run concurrently when called. The caller is returned controlled right away and the method runs in the background.

require 'eldritch'

# define an async method
async def send_email(email)
  # ...
end

# ...
send_email(some_email) # runs in the background
# ...

async blocks

Async blocks are run concurrently.

require 'eldritch'

# do some work
async do
  # some long running task ...
end
# continue working

tasks

Async blocks and async methods both return tasks. These can be used to interact with the async block/method. As of now, you can wait for the task to finish or you can get its return value.

require 'eldritch'

task = async do
  # calculate something that will take a long time
end

# ...

# now we need to result of the task
res = 2 + task.value # waits for the task to finish

together blocks

Together blocks are used to control all async blocks and methods within them as a group. Before exiting, together blocks wait for all their async calls to be done before returning.

require 'eldritch'

together do
  1000.times do
    async do
      # do some work
    end
  end
end
# all 1000 tasks are done

These blocks can also take an argument. This argument is a group that can be used to control the async calls in the block. See the documentation for Eldritch::Group for more information.

require 'eldritch'

together do |group|
  5.times do
    async do
      # do something
      group.interrupt if some_condition  # stops all other tasks
    end
  end
end

Installation

Add this line to your application's Gemfile:

gem 'eldritch'

And then execute:

$ bundle

Or install it yourself as:

$ gem install eldritch