Synchronous deferred operations with fibers (coroutines)
Ruby
Latest commit 6e75bb9 Sep 13, 2012 @godfat bump and prepare release
Permalink
Failed to load latest commit information.
lib do nothing if there's no tasks at all Sep 13, 2012
task @ 066a63c update gemgem Feb 23, 2012
test
.gitignore init and first working version Feb 23, 2012
.gitmodules
.travis.yml .travis.yml: don't test 1.9.2 and use rbx-head and jruby-head Sep 13, 2012
CHANGES.md bump and prepare release Sep 13, 2012
Gemfile
LICENSE init and first working version Feb 23, 2012
README.md README.md: emphasize eventmachine first Mar 21, 2012
Rakefile
sync-defer.gemspec

README.md

sync-defer Build Status

by Lin Jen-Shin (godfat)

LINKS:

DESCRIPTION:

Synchronous deferred operations with fibers (coroutines)

REQUIREMENTS:

  • Either eventmachine or cool.io
  • Ruby 1.9+ (or if fibers could be used in Ruby 1.8)

INSTALLATION:

gem install sync-defer

SYNOPSIS:

Remember to wrap a fiber around the client, and inside the client:

  • Generic interface which would select underneath reactor automatically:

        # Single computation:
        puts(SyncDefer.defer{
                               sleep(5) # any CPU-bound operations
                               100})    # 100
        puts "DONE"
    
        # Multiple computations:
        puts(SyncDefer.defer(lambda{
                               sleep(5) # any CPU-bound operations
                               100
                             },
                             lambda{
                               sleep(2) # any CPU-bound operations
                               50}))    # [100, 50] # it would match the index
        puts "DONE"

    Full examples with reactor turned on:

  • with eventmachine:

        EM.run{
          Fiber.new{
            # or EM::SyncDefer
            SyncDefer.defer{ sleep(5) }
            puts "DONE"
            EM.stop
          }.resume
        }
  • with cool.io:

        # only for adding at least one watcher in the loop
        Coolio::TimerWatcher.new(1).
          attach(Coolio::Loop.default).on_timer{detach}
    
        Fiber.new{
          # or Coolio::SyncDefer
          SyncDefer.defer{ sleep(5) }
          puts "DONE"
        }.resume
        Coolio::Loop.default.run
  • No problems with exceptions, use them as normal:

        EM.run{
          Fiber.new{
            begin
              SyncDefer.defer{ raise "BOOM" }
            rescue => e
              p e
            end
            EM.stop
          }.resume
        }

CONTRIBUTORS:

  • Lin Jen-Shin (@godfat)

LICENSE:

Apache License 2.0

Copyright (c) 2012, Lin Jen-Shin (godfat)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.