SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
Ruby Concurrency & Threads
Anup Nivargi
I <3 Ruby!
Concurrency V Parallelism
Ruby Implementations
MRI, JRuby, Rubinius etc..
Threads In Ruby
thread_example.rb
def thread1
100.times do |i|
puts "In Thread 1"
end
end
def thread2
100.times do
puts "In Thread 2"
end
end
t1 = Thread.new { thread1 }
t2 = Thread.new { thread2 }
t1.join
t2.join
CPU Intensive Tasks
factorial.rb
require 'benchmark'
def factorial_with_range(range)
(1..range).to_a.each_slice(range/4).to_a.each do |elements|
elements.each {|element| factorial(element) }
end
end
def factorial_with_range_threaded(range)
threads = []
(1..range).to_a.each_slice(range/4).to_a.each do |elements|
threads << Thread.new do
elements.each{|element| factorial(element) }
end
end
threads.each(&:join)
end
factorial.rb (contd)
def factorial(n)
(1..n).to_a.inject(:*)
end
Benchmark.bm(30) do |bm|
[4000, 5000].each do |num|
bm.report("Single Threaded #{num}") do
factorial_with_range(num)
end
bm.report("Multi Threaded #{num}") do
factorial_with_range_threaded(num)
end
end
end
Ruby MRI Benchmarks
user system total real
Single Threaded 4000 10.230000 0.100000 10.330000 ( 10.419738)
Multi Threaded 4000 10.340000 0.330000 10.670000 ( 10.770526)
Single Threaded 5000 18.960000 0.490000 19.450000 ( 19.601181)
Multi Threaded 5000 19.120000 0.730000 19.850000 ( 20.012569)
Rubinius Benchmarks
user system total real
Single Threaded 4000 6.228000 0.139000 6.367000 ( 6.413544)
Multi Threaded 4000 0.003000 0.000000 0.003000 ( 4.565215)
Single Threaded 5000 11.519000 0.306000 11.825000 ( 11.937241)
Multi Threaded 5000 0.002000 0.000000 0.002000 ( 9.129658)
Why?
GIL or GVL
in Ruby MRI
But, there is a catch!
IO Intensive Task
server.rb
require "webrick"
server = WEBrick::HTTPServer.new :Port => 8000
server.mount_proc "/" do |req, res|
sleep 1
res.body = "Hello World"
end
trap 'INT' do server.shutdown end
server.start
service.rb
require "benchmark"
require "net/http"
def fetch(count)
uri = URI.parse("http://localhost:8000/")
threads = []
count.times do
threads << Thread.new do
Net::HTTP.get_response(uri)
end
end
threads.each(&:join)
end
Benchmark.bm(30) do |bm|
bm.report("IO Service"){ fetch(20) }
end
Rubinius Benchmarks
user system total real
IO Service 0.014000 0.002000 0.016000 ( 1.037461)
Ruby MRI Benchmarks
user system total real
IO Service 0.050000 0.030000 0.080000 ( 1.070662)
Ahhhh!
Threading Caveats
Conclusion
Online References
Code : https://github.com/anupnivargi/experiments/tree/master/threads
https://blog.engineyard.com/2013/ruby-concurrency
http://speakmy.name/2013/04/02/concurrent-programming-and-threads-in-ruby-reading-list/
https://blog.engineyard.com/2011/ruby-concurrency-and-you
http://merbist.com/2011/02/22/concurrency-in-ruby-explained/
Questions?
Anup Nivargi
@therealnoop
anupnivargi@gmail.com
Thank You!
Ruby Concurrency & Threads

Contenu connexe

Tendances

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi Kant Sahu
 

Tendances (13)

Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Strings
StringsStrings
Strings
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
Python multithreaded programming
Python   multithreaded programmingPython   multithreaded programming
Python multithreaded programming
 
Java String
Java String Java String
Java String
 
String in java
String in javaString in java
String in java
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
 
Java Strings
Java StringsJava Strings
Java Strings
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String handling
String handlingString handling
String handling
 
String Handling
String HandlingString Handling
String Handling
 
parallel programming in tthe PVM-advanced system architecture
parallel programming in tthe PVM-advanced system architectureparallel programming in tthe PVM-advanced system architecture
parallel programming in tthe PVM-advanced system architecture
 

Similaire à Ruby Concurrency & Threads

What's new in Ruby 2.0
What's new in Ruby 2.0What's new in Ruby 2.0
What's new in Ruby 2.0
Kartik Sahoo
 

Similaire à Ruby Concurrency & Threads (20)

Ruby thread safety first
Ruby thread safety firstRuby thread safety first
Ruby thread safety first
 
[Greach 17] make concurrency groovy again
[Greach 17] make concurrency groovy again[Greach 17] make concurrency groovy again
[Greach 17] make concurrency groovy again
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Threads
ThreadsThreads
Threads
 
Threads in Ruby (Basics)
Threads in Ruby (Basics)Threads in Ruby (Basics)
Threads in Ruby (Basics)
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
 
Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)
 
Functional Objects in Ruby: new horizons – Valentine Ostakh
Functional Objects in Ruby: new horizons  – Valentine OstakhFunctional Objects in Ruby: new horizons  – Valentine Ostakh
Functional Objects in Ruby: new horizons – Valentine Ostakh
 
Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojure
 
Clojure concurrency overview
Clojure concurrency overviewClojure concurrency overview
Clojure concurrency overview
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Dataflow: Declarative concurrency in Ruby
Dataflow: Declarative concurrency in RubyDataflow: Declarative concurrency in Ruby
Dataflow: Declarative concurrency in Ruby
 
What's new in Ruby 2.0
What's new in Ruby 2.0What's new in Ruby 2.0
What's new in Ruby 2.0
 
A Case of Accidental Concurrency
A Case of Accidental ConcurrencyA Case of Accidental Concurrency
A Case of Accidental Concurrency
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
ifelse.pptx
ifelse.pptxifelse.pptx
ifelse.pptx
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
Nick Sieger JRuby Concurrency EMRubyConf 2011
Nick Sieger JRuby Concurrency EMRubyConf 2011Nick Sieger JRuby Concurrency EMRubyConf 2011
Nick Sieger JRuby Concurrency EMRubyConf 2011
 
Tackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RTackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in R
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Ruby Concurrency & Threads