No Threads, No Callbacks - Cooperative web servers in Ruby 1.9

July 1, 2016 | Author: igrigorik | Category: N/A
Share Embed Donate


Short Description

Sane async webservers with Ruby 1.9 & Fibers....

Description

|  |   V  V  



     

|  |  

  

       

  ! "

%  #" ! $  #" & |  |  

  

require"active_record´ ActiveRecord::Base.establish_connection( :adapter =>"mysql", :username =>"root", :database =>"database", :pool => 5 ) threads = [] 10.times do |n| threads 'localhost') query =conn.query("select sleep(1)") query.callback { |res| pres.all_hashes } query.errback { |res| pres.all_hashes } puts ´executing«´ end †    † †   † !"#$"%""&'

    ' ) "+

$;$3* )$* V 6 

|  |  

  

| "; " URL, :value =>httpA.response) { |response| caseresponse[:status] whenMemcached::„rrors::NO_„RROR # That's good whenMemcached::„rrors::DISCONN„CT„D # Maybe stop filling the cache for now? else # What could've gone wrong? end } }

http =get(url) memc=save(url, http.response) ifmemc.error? # That's good else # „rr? end

|  |  

 " $* ) V  V   #VV

  

"   4 |  |  

  

. "    ""  = V              VV%

|  |  

  

  V V# V   V   %  V V V #   V V '  %      V ! #      V   V    V"& f=Fiber.new { whiletruedo Fiber.yield"Hi´ end } 2"> pf.resume# => Hi pf.resume# => Hi pf.resume# => Hi

  '(

|  |  

*   "6

%  V V    

  

%    "$ $  

%  $ $  $  

%  V V    

  )* |  |  

  

defquery(sql) f = Fiber.current conn=„ventMachine::MySQL.new(:host =>'localhost') q = conn.query(sql) c.callback { f.resume(conn) } c.errback { f.resume(conn) } return Fiber.yield end

() * " "6

„ventMachine.rundo Fiber.new { res =query('select sleep(1)') puts "Results: #{res.fetch_row.first}" }.resume end

?"""( "     % 

  '(

|  |  

  

defquery(sql) f = Fiber.current conn=„ventMachine::MySQL.new(:host =>'localhost') q = conn.query(sql) c.callback { f.resume(conn) } c.errback { f.resume(conn) } return Fiber.yield end

. *"  "" "

„ventMachine.rundo Fiber.new{ res =query('select sleep(1)') puts "Results: #{res.fetch_row.first}" }.resume end

?"""( "     % 

  '(

|  |  

  

defquery(sql) f=Fiber.current conn=„ventMachine::MySQL.new(:host =>'localhost') q = conn.query(sql) c.callback { f.resume(conn) } c.errback { f.resume(conn) } returnFiber.yield end

â!   "" "

„ventMachine.rundo Fiber.new{ res =query('select sleep(1)') puts "Results: #{res.fetch_row.first}" }.resume end

?"""( "     % 

  '(

|  |  

  

defquery(sql) f=Fiber.current conn=„ventMachine::MySQL.new(:host =>'localhost') q = conn.query(sql) c.callback { f.resume(conn) } c.errback { f.resume(conn) }

´ $   "" "

returnFiber.yield end „ventMachine.rundo Fiber.new{ res =query('select sleep(1)') puts "Results: #{res.fetch_row.first}" }.resume end

?"""( "     % 

  '(

|  |  

  



"    "7  "   $ " % 6

O    

      +       

            +     ,-    

    .    .   .           .          . .   /0    

$;" "$*   "  *  $$"      

|  |  

  

„ventMachine.synchronydo multi =„ventMachine::Synchrony::Multi.new multi.add:a, „ventMachine::HttpRequest.new(url1).aget multi.add:b, „ventMachine::HttpRequest.new(url2).apost res =multi.perform p³No callbacks, and parallel HTTP requests!" p res „ventMachine.stop end

!  
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF