0.0
No commit activity in last 3 years
No release in over 3 years
Grand central dispatch (GCD) emulation for ruby. It allows to easily run asynchronous code.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.2
~> 13.0
~> 3.10

Runtime

 Project Readme

GBDispatch

Library allows to easily dispatch block of code for queues. It is inspired by Grand Central Dispatch behaviour.

Installation

Add this line to your application's Gemfile:

gem 'gb_dispatch'

And then execute:

$ bundle

Or install it yourself as:

$ gem install gb_dispatch

Then in your script

require 'gb_dispatch'

Usage

To dispatch asynchronously

GBDispatch.dispatch_async :my_queue do
    puts 'my code here'
end

for synchronous execution

my_result = GBDispatch.dispatch_sync :my_queue do
    puts 'my code here'
end

for delay execution

delay = 4.5 #seconds
GBDispatch.dispatch_after delay, :my_queue do
    puts 'my code here'
end

Using with Rails

If you are using Rails, all blocks are wrapped in connection pool, so you don't need to worry about creating and removing new connections. Only thing you need to do, is to increase connection pool size by number of cores of your machine.

How it works

Queues

For each created queue there is a new thread created. However this threads are used only for scheduling purpose. All code execution happens on execution pool.

Queues ensure order of execution, but they don't guarantee that all blocks from one queue will be executed on the same thread.

Execution pool

This is pool of thread where your code will be executed. Amount of threads in the pool match the amount of cores of your machine (except if you have only one core, there will be 2 threads).

Exceptions

GBDispatch is designed in the way, that if there is exception thrown it will not crash the whole app/script. All exceptions will be logged. If you use +#dispatch_sync+ method, thrown exception will be returned as a result of your block.

Contributing

  1. Fork it ( https://github.com/GenieBelt/gb_dispatch/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request