Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Modern Concurrency Practices in Ruby
Search
Arnab Deka
June 20, 2014
Technology
81
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Modern Concurrency Practices in Ruby
Presented at RuLu'14:
http://rulu.eu/speakers/#arnab
Arnab Deka
June 20, 2014
Transcript
67 slides
Modern Concurrency Practices in Ruby @arnab_deka
None
None
None
None
Agenda I would remove the thread and add actors or
some other more advanced concurrency features — Matz
Agenda... 4 Elixir 4 Clojure 4 Go 4 Of course,
Ruby ... 4 Even some French!
Agenda... 4 Elixir 4 Clojure 4 Go 4 Of course,
Ruby ... 4 Even some French! Sadly, NO Pascal
Arnab 4 @arnab_deka 4 http://arnab-deka.com/ 4 Assam > Kerala >
Pune > NYC > Seattle > Bangalore 4 Infosys/Goldman > Amazon > LivingSocial 4 C/C++ > Perl > Java > Ruby > Ruby/Clojure
None
None
None
Warm up class Counter attr_reader :count def initialize @count =
0 end def inc @count += 1 end end
Warm up... counter = Counter.new t1 = Thread.new { 1000.times
{ counter.inc } } t2 = Thread.new { 1000.times { counter.inc } } t1.join; t2.join puts "Final count: #{counter.count}"
Solution? Mutexes! Of course!
Solution? Mutexes! But... 4 testing? 4 debugging? 4 waking up
at 3 AM to solve that Race condition?
None
STM
STMs: sidetrack on atoms 4 Compare and Swap 4 Lock-free
4 Non-blocking
None
STMs: sidetrack on atoms... require 'atomic' class Counter def initialize
@count = Atomic.new(0) end def inc @count.update { |num| num + 1 } end def count @count.value end end
STMs: sidetrack on atoms... counter = Counter.new t1 = Thread.new
{ 1000.times { counter.inc } } t2 = Thread.new { 1000.times { counter.inc } } t1.join; t2.join puts "Final count, using atomic: #{counter.count}"
STM... (def transfer-count (atom 0)) (let [fn-nonnegative #(>= % 0)]
(def alice (ref 1000 :validator fn-nonnegative)) (def bob (ref 2000 :validator fn-nonnegative)))
STM... (defn transfer [from to amount] (dosync (swap! transfer-count inc)
(alter from - amount) (alter to + amount)))
STM... (repeatedly 25 #(transfer alice bob 100)) => IllegalStateException (println[@alice
@bob @transfer-count]) ;; => [0 3000 11]
STM... ✔Atomic
STM... ✔Atomic ✔Consistent
STM... ✔Atomic ✔Consistent ✔Isolated
STM... ✔Atomic ✔Consistent ✔Isolated But... ✘ Durable
STM... 4 Synchronous 4 Retries...
STMs in Ruby??? :(
STMs in Ruby??? Alternatives... 4 HTM 4 Intel's Transactional Synchronization
Extensions 4 PyPy's Transactional Memory 4 Atoms
counting.rubis classe Compteur attr_lire :recenser définir initialiser @recenser = 0
fin définir accroissement @recenser += 1 fin fin
Step 1 1763: French Indian War
Step 2 East India Companies: French > Brits
None
None
French in the US and in India... 4 ENIEO, not
ENIAC 4 La sous-traitance, not "outsourcing" 4 ... 4 ... 4 ... 4 "Le Rubis", not Ruby
None
Actors
WHAT OOP WAS MEANT TO BE.
Actors in Elixir defmodule Player do def play(name) do receive
do {:serve} -> # do it ... end end end p1 = spawn_link(Player, :play, ["Djokovic"]) send(p1, {:serve}) # or register to a name Process.register(p1, :djoker) send(:djoker, {:serve})
Actors in Ruby class Player include Celluloid def initialize(name); end
def serve; end end djoker = Player.new djoker.async.serve Player.supervise_as(:djoker, "Djokovic") Celluloid::Actor[:djoker].async.serve
Erlang/Elixir: there's more... 4 Processes are very very light-weight
Erlang/Elixir: there's more... 4 Processes are very very light-weight 4
fault-tolerance: let it crash
Erlang/Elixir: there's more... 4 Processes are very very light-weight 4
fault-tolerance: let it crash 4 OTP framework
Erlang/Elixir: there's more... 4 Processes are very very light-weight 4
fault-tolerance: let it crash 4 OTP framework 4 Distributed
What about Ruby? 4 Celluloid 4 DCell
Actors in Ruby: DCell 4 Over 0mq require 'dcell' DCell.start
:id => "itchy", :addr => "tcp://127.0.0.1:9001" DCell.start :id => "scratchy", :addr => "tcp://127.0.0.1:9002" itchy_node = DCell::Node["itchy"]
Channels
Actors 4 focus: named processes/actors 4 coupled to message format
Channels 4 focus: transport, not receiver 4 anything can produce/consume
side-track: #golang board := make([][]string, m) for i := range
board { board[i] = make([]string, n) }
side-track: #golang board = Array.new(Array.new)
core.async (def c (chan)) (thread (println "Pouring: " (<!! c)
" from channel")) (>!! c "Hello RuLu")
core.async: buffering 4 unbuffered by default. Will block on 4
reading from empty channels 4 writing to non-empty channels
core.async: buffering 4 will buffer upto n: (def bc (chan
20)) (>!! bc "Hello") (>!! bc "RuLu") (<!! bc) ;;; "Hello" (<!! bc) ;;; "RuLu"
core.async: buffering 4 dropping channels (def c (chan (dropping-buffer 20)))
4 sliding channels (def c (chan (sliding-buffer 20))) 4 Why not handle buffering automatically?
core.async: channel functions 4 map-chan 4 onto-chan 4 ...
core.async: go blocks (def c (chan)) (go (let [x (<!
c) y (<! c)] (println (clojure.string/join " " [x y])))) (>!! c "Hello") (>!! c "RuLu!")
None
Channels in Ruby require 'jo' include Jo c = chan
jo do x = c.take y = c.take puts (x + y) end c << 10 c << 25
Closing thoughts & Advice
Experiment
Keep it simple
None
Resources 4 github: arnab/modern-concurrency-rulu 4 presentation/resources.md
RuLu
More Decks by Arnab Deka
See All by Arnab Deka
Requiem for a dream
orknob
0
69
Other Decks in Technology
See All in Technology
Del vibe coding a un Desarrollo (agéntico) fiable
jcabot
0
220
成長するID管理基盤における運用もコストも犠牲にしないオブザーバビリティ向上の実践 / Improving Observability for a Growing Identity Platform Without Adding Operational Burden or Cost
kaminashi
1
130
Test Impact Analysis を武器に立ち向かう、AI 時代のテスト増加に対するヌーラボのアプローチ / getting-started-datadog-test-impact-analysis
k6s4i53rx
3
420
その修正で本当に速くなったのか? k6とOpenTelemetryで確かめる性能改善 / Performance review with k6 and OpenTelemetry
ymotongpoo
3
440
セキュリティチームは どのコンテナイメージの脆弱性対応を優先すべきか 〜CISA新指針とSCCで作る脆弱性トリアージ〜
swdtkuy
0
330
Web Performance Is a Journey [PerfNow 2026]
tammyeverts
2
150
設計手法を「道具」として使いこなすために
a_suenami
7
1.8k
新規事業開発でのAWS活用成功談&失敗談! 〜新規事業開発を専門に行うRelicならではのAWS活用〜
amixedcolor
1
140
クラウドでも使えるNetApp -現場目線での設計とクラウドバックアップの活用
ryutakondo
1
330
みん強Lightning! データ基盤ひとりでできるかな
endonanana
0
200
メルカリの小規模チーム「Pod」で、 エンジニアがデータアナリストなしで、 AIを使ってデータ分析の自走を目指す取り組み
jackojacko_
1
350
「個社」ではなく「業界」の課題を解く AI 対話開発 ー 価値を広げる設計と、価値を守るエンジニアリング ー
ivry_presentationmaterials
4
870
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
A Modern Web Designer's Workflow
chriscoyier
699
190k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
400
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
520
What's in a price? How to price your products and services
michaelherold
247
13k
30 Presentation Tips
portentint
PRO
1
480
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
1
250
The browser strikes back
jonoalderson
0
2k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k