Skip to content

Instantly share code, notes, and snippets.

@lukaszkorecki
Created January 31, 2013 21:16
Show Gist options
  • Save lukaszkorecki/4686533 to your computer and use it in GitHub Desktop.
Save lukaszkorecki/4686533 to your computer and use it in GitHub Desktop.
V8 + Sinatra + Fibers + EM
require 'sinatra'
require 'fiber'
require 'v8'
require 'rack/fiber_pool'
require 'eventmachine'
class Evaluator
include EM::Deferrable
def initialize exp
@log = ::Logger.new STDOUT
@exp = exp
end
def run
@log.info ">>> Running"
ctx = ::V8::Context.new
val = ctx.eval @exp
@log.info ">>> VAL: #{val}"
self.succeed val
@log.info ">>> DISPOSING"
ctx.dispose
end
end
class App < Sinatra::Base
use Rack::FiberPool, size: 100
set :logging, true
get "/" do
logger.info "ok"
ev = Evaluator.new "1+2"
f = Fiber.current
ev.callback do |val|
logger.info "from callback"
f.resume val
end
EM.next_tick do
logger.info "scheduling"
ev.run
end
v = Fiber.yield
logger.info "v: #{v}"
body v.to_s
end
end
require './app'
run App
source :rubygems
gem 'therubyracer'
gem 'rack-fiber_pool'
gem 'sinatra'
gem 'thin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment