Skip to content
Synchronous deferred operations with fibers (coroutines)
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
task @ 066a63c
test
.gitignore
.gitmodules
.travis.yml
CHANGES.md
Gemfile
LICENSE
README.md
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.

Something went wrong with that request. Please try again.