项目作者: dotboris

项目描述 :
A ruby DSL that adds concurrent programming constructs to make parallelism easier.
高级语言: Ruby
项目地址: git://github.com/dotboris/eldritch.git
创建时间: 2014-03-20T00:52:13Z
项目社区:https://github.com/dotboris/eldritch

开源协议:MIT License

下载


Eldritch

Build Status
Coverage Status
Code Climate

The dark arts of concurrent programming.

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

Usage

  1. Install it gem install eldritch
  2. Require it require 'eldritch'
  3. Use it (see features below)

By default eldritch will inject the DSL into the global scope. If you don’t want
this, you can require eldritch/safe instead of eldritch.

  1. require 'eldricth/safe'
  2. class MyClass
  3. include Eldritch::DSL
  4. extend Eldritch::DSL
  5. # The DSL is available in this class
  6. end

Development

Setup

  1. bundler install

Running tests

  1. bundler exec rake

Running examples

  1. ruby -I lib examples/{your favorite example}.rb

Generate doc

  1. bundle exec rake doc

Features

async methods

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

  1. require 'eldritch'
  2. # define an async method
  3. async def send_email(email)
  4. # ...
  5. end
  6. send_email(some_email) # runs in the background

ruby 1.9.3 and 2.0.0

For all versions of ruby before 2.1.0, you need to define async methods like so:

  1. def foo
  2. # stuff
  3. end
  4. async :foo

Since ruby 2.1.0, def returns the name of the method defined as a symbol. This
allows for the cleaner async def foo syntax.

async blocks

Async blocks are run concurrently.

  1. require 'eldritch'
  2. async do
  3. # runs in the background
  4. end

tasks

Async blocks and async methods both return tasks. These can be used to interact
with the async block/method.

  1. require 'eldritch'
  2. task = async do
  3. # calculate something that will take a long time
  4. end
  5. # we need to result of the task
  6. 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.

  1. require 'eldritch'
  2. together do
  3. 1000.times do
  4. async do
  5. # do some work
  6. end
  7. end
  8. end
  9. # 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.

  1. require 'eldritch'
  2. together do |group|
  3. 5.times do
  4. async do
  5. # do something
  6. group.interrupt if some_condition # stops all other tasks
  7. end
  8. end
  9. end

A note on GIL

MRI has this nasty little feature called a GIL or Global Interpreter Lock.
This lock makes it so that only one thread can run at a time. Let’s say that you
have 4 cores, running threaded code on MRI will only make use of 1 core.
Sometimes, you might not gain a speed boost if you make code parallel. This
could the case even if theory says otherwise.

Not all ruby implementations use a GIL. For example, jRuby does not use a
GIL.

If your ruby implementation has a GIL, you will probably see a speed boost if
your code does a lot of IO or anything that’s blocking. In that case running on
a single core is not that much of a hindrance, because most of the threads will
be blocked and your code should run more often.