Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Concorrência, paralelismo e o Ruby

Concorrência, paralelismo e o Ruby

Algumas considerações sobre criação de aplicações Ruby usando conceitos de concorrência e paralelismo.
Apresentação feita em 08/12/2012 no Rupy Brasil.

Ricardo Valeriano

December 10, 2012
Tweet

More Decks by Ricardo Valeriano

Other Decks in Programming

Transcript

  1. require 'benchmark' require 'net/http' require 'uri' uri = URI("http://example.org") Benchmark.bm

    do |b| b.report('get normal') do 10.times { Net::HTTP.get_response uri } end end Sunday, December 9, 12
  2. require 'benchmark' require 'net/http' require 'uri' uri = URI("http://example.org") Benchmark.bm

    do |b| b.report('get normal') do 10.times { Net::HTTP.get_response uri } end end b.report('get maroto') do 10.times.map do Thread.new{ Net::HTTP.get_response uri } end.map(&:join) end Sunday, December 9, 12
  3. require 'benchmark' require 'mysql2' sql_fuleiro = "SELECT * FROM user"

    Benchmark.bm do |x| x.report "select fuleiro" do 15.times do end end end Sunday, December 9, 12
  4. require 'benchmark' require 'mysql2' sql_fuleiro = "SELECT * FROM user"

    Benchmark.bm do |x| x.report "select fuleiro" do 15.times do end end end mysql2 = Mysql2::Client.new host: "localhost", username: "root" Sunday, December 9, 12
  5. require 'benchmark' require 'mysql2' sql_fuleiro = "SELECT * FROM user"

    Benchmark.bm do |x| x.report "select fuleiro" do 15.times do end end end mysql2 = Mysql2::Client.new host: "localhost", username: "root" mysql2.query "use mysql" Sunday, December 9, 12
  6. require 'benchmark' require 'mysql2' sql_fuleiro = "SELECT * FROM user"

    Benchmark.bm do |x| x.report "select fuleiro" do 15.times do end end end mysql2 = Mysql2::Client.new host: "localhost", username: "root" mysql2.query "use mysql" mysql2.query sql_fuleiro Sunday, December 9, 12
  7. x.report "select threaded" do 15.times.map do Thread.new{ mysql2 = Mysql2::Client.new

    host: "localhost", username: "root" mysql2.query "use mysql" mysql2.query sql_fuleiro } end.map(&:join) end Sunday, December 9, 12
  8. x.report "select event machine" do 15.times do mysql2 = Mysql2::EM::Client.new

    host: "localhost", username: "root" mysql2.query "use mysql" mysql2.query sql_fuleiro end end Sunday, December 9, 12
  9. x.report "select event machine" do 15.times do mysql2 = Mysql2::EM::Client.new

    host: "localhost", username: "root" mysql2.query "use mysql" mysql2.query sql_fuleiro end end Sunday, December 9, 12
  10. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end Sunday, December 9, 12
  11. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end Sunday, December 9, 12
  12. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new Sunday, December 9, 12
  13. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new (1..100).map do Thread.new{ } end Sunday, December 9, 12
  14. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new (1..100).map do Thread.new{ } end 100.times { omg.pluzz(1) } Sunday, December 9, 12
  15. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new (1..100).map do Thread.new{ } end 100.times { omg.pluzz(1) } .map(&:join) Sunday, December 9, 12
  16. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new Sunday, December 9, 12
  17. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new Sunday, December 9, 12
  18. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new omg.pluzz 1 Sunday, December 9, 12
  19. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter omg.pluzz 1 Sunday, December 9, 12
  20. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 omg.pluzz 1 Sunday, December 9, 12
  21. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 omg.pluzz 1 Sunday, December 9, 12
  22. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= omg.pluzz 1 Sunday, December 9, 12
  23. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 omg.pluzz 1 Sunday, December 9, 12
  24. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 Sunday, December 9, 12
  25. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 Sunday, December 9, 12
  26. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 Sunday, December 9, 12
  27. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter Sunday, December 9, 12
  28. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 Sunday, December 9, 12
  29. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 Sunday, December 9, 12
  30. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 @counter Sunday, December 9, 12
  31. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 @counter 2 Sunday, December 9, 12
  32. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 @counter 2 2 + 1 Sunday, December 9, 12
  33. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 @counter 2 2 + 1 @counter= Sunday, December 9, 12
  34. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 @counter 2 2 + 1 @counter= 3 Sunday, December 9, 12
  35. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 2 + 1 @counter 2 2 + 1 @counter= 3 Sunday, December 9, 12
  36. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 2 + 1 @counter= @counter 2 2 + 1 @counter= 3 Sunday, December 9, 12
  37. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end omg = SomatorOMG.new @counter 0 0 + 1 @counter= 1 @counter 1 1 + 1 @counter= 2 @counter 2 2 + 1 @counter= 3 @counter 2 2 + 1 @counter= 3 Sunday, December 9, 12
  38. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end require 'monitor' Sunday, December 9, 12
  39. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end require 'monitor' include MonitorMixin Sunday, December 9, 12
  40. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end require 'monitor' include MonitorMixin super Sunday, December 9, 12
  41. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end require 'monitor' include MonitorMixin super # WAT? Sunday, December 9, 12
  42. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end require 'monitor' include MonitorMixin super # WAT? .. extend(MonitorMixin) Sunday, December 9, 12
  43. class SomatorOMG attr_accessor :counter def initialize @counter = 0 end

    def pluzz(how_much) @counter = @counter + how_much end end require 'monitor' include MonitorMixin synchronize do end super # WAT? .. extend(MonitorMixin) Sunday, December 9, 12
  44. Yukihiro Matsumoto, RubyConf 2012 Yukihiro Matsumoto, RubyConf 2012 “i’m not

    a threading guy” “i’m not a threading guy” Sunday, December 9, 12
  45. do puts "OMG! Nasci!" end pid = Process.fork puts "#{pid},

    eu (#{Process.pid}) sou seu pai! XD" Sunday, December 9, 12
  46. do puts "OMG! Nasci!" end pid = Process.fork while true

    sleep(1) end puts "#{pid}, eu (#{Process.pid}) sou seu pai! XD" Sunday, December 9, 12
  47. do puts "OMG! Nasci!" end pid = Process.fork while true

    sleep(1) end while true sleep(1) end puts "#{pid}, eu (#{Process.pid}) sou seu pai! XD" Sunday, December 9, 12
  48. rupy = "Rupy BR" Process.fork do end rupy << "

    2012" puts "no filho: #{rupy}" Sunday, December 9, 12
  49. rupy = "Rupy BR" Process.fork do end rupy << "

    2012" puts "no filho: #{rupy}" puts "no pai: #{rupy}" Sunday, December 9, 12
  50. pid = Process.fork do end reader, writer = IO.pipe while(

    (msg = gets.chomp) != "fui") writer.puts "Papa: #{msg}" end Sunday, December 9, 12
  51. pid = Process.fork do end reader, writer = IO.pipe while(

    (msg = gets.chomp) != "fui") writer.puts "Papa: #{msg}" end puts "fui papa!" Sunday, December 9, 12
  52. pid = Process.fork do end reader, writer = IO.pipe while(

    (msg = gets.chomp) != "fui") writer.puts "Papa: #{msg}" end puts "fui papa!" while(msg = reader.gets) puts msg end Sunday, December 9, 12
  53. pid = Process.fork do end reader, writer = IO.pipe while(

    (msg = gets.chomp) != "fui") writer.puts "Papa: #{msg}" end puts "fui papa!" while(msg = reader.gets) puts msg end writer.close Sunday, December 9, 12
  54. pid = Process.fork do end reader, writer = IO.pipe while(

    (msg = gets.chomp) != "fui") writer.puts "Papa: #{msg}" end reader.close puts "fui papa!" while(msg = reader.gets) puts msg end writer.close Sunday, December 9, 12
  55. reader, writer = IO.pipe armenga = Armenga.new armenga.lol = "LoLoLoLing"

    writer.puts Marshal::dump armenga Sunday, December 9, 12
  56. reader, writer = IO.pipe pid = Process.fork do end armenga

    = Armenga.new armenga.lol = "LoLoLoLing" writer.puts Marshal::dump armenga Sunday, December 9, 12
  57. reader, writer = IO.pipe pid = Process.fork do end armenga

    = Armenga.new armenga.lol = "LoLoLoLing" writer.puts Marshal::dump armenga reader.close Sunday, December 9, 12
  58. reader, writer = IO.pipe writer.close data = reader.gets omg =

    Marshal::load data pid = Process.fork do end armenga = Armenga.new armenga.lol = "LoLoLoLing" writer.puts Marshal::dump armenga reader.close Sunday, December 9, 12
  59. reader, writer = IO.pipe writer.close data = reader.gets omg =

    Marshal::load data pid = Process.fork do end armenga = Armenga.new armenga.lol = "LoLoLoLing" writer.puts Marshal::dump armenga reader.close puts omg.lol puts omg.sera? Sunday, December 9, 12
  60. #!/usr/bin/env ruby # # server_1 require 'rubygems' require 'eventmachine' module

    EchoServer def post_init puts "-- someone connected to the echo server!" end def receive_data data send_data ">>> you sent: #{data}" end end EventMachine::run { EventMachine::start_server "127.0.0.1", 8081, EchoServer puts 'running echo server on 8081' } Sunday, December 9, 12
  61. #!/usr/bin/env ruby # # server_1 require 'rubygems' require 'eventmachine' module

    EchoServer def post_init puts "-- someone connected to the echo server!" end def receive_data data send_data ">>> you sent: #{data}" end end EventMachine::run { EventMachine::start_server "127.0.0.1", 8081, EchoServer puts 'running echo server on 8081' } Sunday, December 9, 12
  62. #!/usr/bin/env ruby # # client_1 require 'rubygems' require 'eventmachine' class

    Echo < EventMachine::Connection def post_init send_data 'Hello' end def receive_data(data) p data end end EventMachine.run { EventMachine.connect '127.0.0.1', 8081, Echo } Sunday, December 9, 12
  63. #!/usr/bin/env ruby # # client_1 require 'rubygems' require 'eventmachine' class

    Echo < EventMachine::Connection def post_init send_data 'Hello' end def receive_data(data) p data end end EventMachine.run { EventMachine.connect '127.0.0.1', 8081, Echo } Sunday, December 9, 12
  64. >> future = charlie.future.report class Charlie def report # some

    sloooooow process here. end end Sunday, December 9, 12
  65. >> future = charlie.future.report => #<Celluloid::Future:0x000001009759b8> >> future.value class Charlie

    def report # some sloooooow process here. end end Sunday, December 9, 12
  66. >> future = charlie.future.report => #<Celluloid::Future:0x000001009759b8> >> future.value => "Charlie

    Sheen is winning!" class Charlie def report # some sloooooow process here. end end Sunday, December 9, 12
  67. >> future = charlie.future.report => #<Celluloid::Future:0x000001009759b8> >> future.value => "Charlie

    Sheen is winning!" class Charlie def report # some sloooooow process here. end end include Celluloid Sunday, December 9, 12