Async

library of simple asynchronous utilities for ruby

Description

I've found myself using the same patterns fo adding lightweight concurrency and parallelism in my work. This gem is a port of those strategies to both centralize them and share them with others.

Installation

gem 'async_rb'

Usage

#####
# Async::Base - Add easy asynchronous processing of methods similar to Go or Celluloid
#####
class MyClass
  include Async::Base

  def say_hello
    puts "hello"
  end
end

obj = MyClass.new

# Run #say_hello synchronously
obj.say_hello
=> nil

# Run #say_hello asynchronously
obj.async(&:say_hello)
=> #<Thread:0x007fea3c8f5858 run> 


#####
# Async::Runner - Easily switch between Threads, Forks, and Synchronous processing with a common interface
#####
# Strategies include :thread, :fork, and :synchronous
# Defaults to :thread
runner = Async::Runner.new(:fork)
[
  runner.run { `rake db:migrate` },
  runner.run { `echo hello world` }
].each(&:join)