Ruby hollywood

30
Ruby goes to Hollywood @elise_huard LRUG Tuesday 12 April 2011

description

 

Transcript of Ruby hollywood

Page 1: Ruby hollywood

Rubygoes to

Hollywood@elise_huard LRUG

Tuesday 12 April 2011

Page 2: Ruby hollywood

Why Concurrency?

Tuesday 12 April 2011

Page 3: Ruby hollywood

“... for the first time in history, no one is building a much faster sequential processor. If you want your programs to run significantly faster (...) you’re going to have to parallelize your program.”

Hennessy and Patterson

“Computer Architectures” (4th edition, 2007)

Tuesday 12 April 2011

Page 4: Ruby hollywood

Concurrent programs

!=Parallel computing

Tuesday 12 April 2011

Page 5: Ruby hollywood

3 threads, 2 cores

1

2

3

1

1

2

3 1

23

core1 core2 core1 core2

Tuesday 12 April 2011

Page 6: Ruby hollywood

language VM

OS(kernel processes, other processes)

Your program

multicore - multiCPU

Tuesday 12 April 2011

Page 7: Ruby hollywood

Concurrency will melt your brain

non-determinism

shared state

Tuesday 12 April 2011

Page 8: Ruby hollywood

Actor Model

Tuesday 12 April 2011

Page 9: Ruby hollywood

no shared statein response to a message that it receives, an actor can:make local decisionscreate more actorssend more messagesdetermine how to respond to the next message received

Hewitt et al. 1973

Tuesday 12 April 2011

Page 10: Ruby hollywood

Inspiration

Artificial Intelligence

quantum mechanics - we cannot observe the details by which the order of arrival of messages for an actor is determined. attempting to do so can affect result and push indeterminacy elsewhere.

Tuesday 12 April 2011

Page 11: Ruby hollywood

only actors

no global state

partial order of execution

The point

Tuesday 12 April 2011

Page 12: Ruby hollywood

Actors in Ruby

Tuesday 12 April 2011

Page 13: Ruby hollywood

definitely not parallel

Tuesday 12 April 2011

Page 14: Ruby hollywood

Rubinius Actors

require 'actor'

actor = Actor.spawn(Actor.current) do |master| msg = Actor.receive master.send msgend

actor.send "test"msg = Actor.receive# msg == "test"

Tuesday 12 April 2011

Page 15: Ruby hollywood

Revactor

sock = Revactor::TCP.connect(host, port)listener = Revactor::TCP.listen(addr, port)sock = listener.accept

sock.controller = Actor.currentsock.active = trueActor.receive do |filter| filter.when(Case[:tcp, sock, Object]) do |_, _, data| ... end filter.when(Case[:somethingelse, Object]) do |_, message| ... endend

Tuesday 12 April 2011

Page 16: Ruby hollywood

Concurrent gem

theoretical - different concurrency models

use rubinius style actors in other versions of Ruby

https://github.com/mental/concurrent

Tuesday 12 April 2011

Page 17: Ruby hollywood

Meh.

Tuesday 12 April 2011

Page 18: Ruby hollywood

Cheat on Ruby

Tuesday 12 April 2011

Page 19: Ruby hollywood

Erlang

light-weight processes

= actors

Tuesday 12 April 2011

Page 20: Ruby hollywood

Erlectricity

require 'rubygems'

require 'erlectricity'

receive do |f| f.when([:echo, String]) do |text| f.send!([:result, "You said: #{text}"]) f.receive_loop endend

https://github.com/mojombo/erlectricity

Tuesday 12 April 2011

Page 21: Ruby hollywood

Erlectricity

-module(echo).-export([test/0]).

test() -> Cmd = "ruby echo.rb", Port = open_port({spawn, Cmd}, [{packet, 4}, nouse_stdio, exit_status, binary]), Payload = term_to_binary({echo, <<"hello world!">>}), port_command(Port, Payload), receive {Port, {data, Data}} -> {result, Text} = binary_to_term(Data), io:format("~p~n", [Text]) end.

Tuesday 12 April 2011

Page 22: Ruby hollywood

BERT RPC

BERT (Binary ERlang Term) serialization library for Ruby.

convert data into erlang binary format before sending them over the wire

Tuesday 12 April 2011

Page 23: Ruby hollywood

Scala

“We also included enhancements to make it easier to call from Ruby into Scala libraries, which has enabled the Lift web framework to offer support for Ruby.” Charles Nutter, ‘JRuby 1.6 released, now what ?’

http://www.engineyard.com/blog/2011/jruby-1-6-released-now-what/

Actors in standard library

Tuesday 12 April 2011

Page 24: Ruby hollywood

Akka

https://github.com/danielribeiro/RubyOnAkka

Tuesday 12 April 2011

Page 25: Ruby hollywood

Back to Ruby

Tuesday 12 April 2011

Page 26: Ruby hollywood

Time to control shared state ?

Change RubySpec ?

default variables: not shared between threads

have keyword for shared variables

Tuesday 12 April 2011

Page 27: Ruby hollywood

Let’s think about it.

Tuesday 12 April 2011

Page 28: Ruby hollywood

Thanks@elise_huard

Tuesday 12 April 2011

Page 29: Ruby hollywood

Ruby goes to

HollywoodElise Huard LRUG 11-04-2011

Tuesday 12 April 2011

Page 30: Ruby hollywood

Ruby goes to

HollywoodElise Huard LRUG

Tuesday 12 April 2011