Project

sync-defer

0.0
No commit activity in last 3 years
No release in over 3 years
Synchronous deferred operations with fibers (coroutines)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

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.