Created
January 31, 2013 21:16
-
-
Save lukaszkorecki/4686533 to your computer and use it in GitHub Desktop.
V8 + Sinatra + Fibers + EM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './app' | |
run App |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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