Last active
March 6, 2016 08:55
-
-
Save arnab/9021588 to your computer and use it in GitHub Desktop.
Used in my talk at RubyConf AU: https://slid.es/arnab_deka/modern-concurrency-practises-in-ruby/
This file contains 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
defmodule Player do | |
def loop(name, other_player, phrase) do | |
receive do | |
{:serve} -> | |
IO.puts "#{name}: serving" | |
send(other_player, {:play_next, 1}) | |
loop(name, other_player, phrase) | |
{:play_next, rally_count} -> | |
:timer.sleep(700) | |
:random.seed(:os.timestamp()) | |
percentage_shot = :random.uniform(20) | |
unenforced_error = 0.1 * rally_count > percentage_shot | |
shot = if :random.uniform(2) > 1 do "backhand" else "forehand" end | |
if unenforced_error do | |
IO.puts "##{rally_count}: #{name}: Noooooo!" | |
send(other_player, {:celebrate_point}) | |
else | |
IO.puts "##{rally_count}: #{name}: #{shot}" | |
send(other_player, {:play_next, rally_count + 1}) | |
end | |
loop(name, other_player, phrase) | |
{:celebrate_point} -> | |
IO.puts("######## #{name}: #{phrase} #######") | |
:timer.sleep(500) | |
send(self, {:serve}) | |
loop(name, other_player, phrase) | |
_ -> | |
IO.puts("I only know how to play tennis.") | |
loop(name, other_player, phrase) | |
end | |
end | |
end | |
p1 = spawn_link(Player, :loop, ["Federer", :rafa, "Come on!"]) | |
Process.register(p1, :federer) | |
p2 = spawn_link(Player, :loop, ["Rafa", :federer, "Vamos!"]) | |
Process.register(p2, :rafa) | |
send(:federer, {:serve}) | |
receive do | |
{:EXIT, pid, reason} -> IO.puts "#{pid} exited. Said: #{reason}." | |
end |
This file contains 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 "celluloid" | |
class Player | |
include Celluloid | |
attr_writer :other_player | |
def initialize(name, phrase, other_player=nil) | |
@name = name | |
@phrase = phrase | |
@other_player = other_player | |
end | |
def serve | |
puts "#{@name}: serving" | |
@other_player.async.play_next(1) | |
end | |
def play_next(rally_count) | |
sleep(0.5) | |
percentage_shot = rand(20).to_i | |
unenforced_error = 0.1 * rally_count > percentage_shot | |
shot = rand(2) > 1 ? "backhand" : "forehand" | |
if unenforced_error | |
puts "##{rally_count}: #{@name}: Noooooo!" | |
@other_player.async.celebrate_point | |
else | |
puts "##{rally_count}: #{@name}: #{shot}" | |
@other_player.async.play_next(rally_count + 1) | |
end | |
end | |
def celebrate_point | |
puts("######## #{@name}: #{@phrase} #######") | |
sleep(0.5) | |
self.async.serve | |
end | |
end | |
federer_manager = Player.supervise_as(:federer, "Federer", "Come on!") | |
rafa_manager = Player.supervise_as(:rafa, "Rafa", "Vamos!", Celluloid::Actor[:federer]) | |
Celluloid::Actor[:federer].other_player = Celluloid::Actor[:rafa] | |
Celluloid::Actor[:federer].async.serve | |
sleep(1000) |
This file contains 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
Federer: serving | |
#1: Rafa: forehand | |
#2: Federer: forehand | |
#3: Rafa: backhand | |
#4: Federer: backhand | |
#5: Rafa: backhand | |
#6: Federer: backhand | |
#7: Rafa: backhand | |
#8: Federer: backhand | |
#9: Rafa: forehand | |
#10: Federer: backhand | |
#11: Rafa: forehand | |
#12: Federer: backhand | |
#13: Rafa: forehand | |
#14: Federer: backhand | |
#15: Rafa: forehand | |
#16: Federer: backhand | |
#17: Rafa: forehand | |
#18: Federer: backhand | |
#19: Rafa: backhand | |
#20: Federer: backhand | |
#21: Rafa: Noooooo! | |
######## Federer: Come on! ####### | |
Federer: serving | |
#1: Rafa: forehand | |
#2: Federer: backhand | |
#3: Rafa: forehand | |
#4: Federer: backhand | |
#5: Rafa: forehand | |
#6: Federer: forehand | |
#7: Rafa: backhand | |
#8: Federer: backhand | |
#9: Rafa: forehand | |
#10: Federer: forehand | |
#11: Rafa: backhand | |
#12: Federer: backhand | |
#13: Rafa: forehand | |
#14: Federer: forehand | |
#15: Rafa: backhand | |
#16: Federer: forehand | |
#17: Rafa: forehand | |
#18: Federer: forehand | |
#19: Rafa: forehand | |
#20: Federer: Noooooo! | |
######## Rafa: Vamos! ####### | |
Rafa: serving | |
#1: Federer: backhand | |
#2: Rafa: forehand | |
#3: Federer: forehand | |
#4: Rafa: backhand | |
^C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment