SlideShare a Scribd company logo
Parallel
Computing
with ruby
My Company
My Company
Challenges
• Learning Ruby
Challenges
• Learning Ruby	

• Rails 2 to Rails 3
Challenges
• Learning Ruby	

• Rails 2 to Rails 3	

• Integrating with Banking APIs
Challenges
What is Parallel
Computing?
Multithreading!
vs!
Multiprocessing
Share Memory Space!
Lightweight!
May Have Memory
Management Issues!
Can Sometimes Take
Advantage of Multiple
CPUs
Thread Process
Separate Memory Space!
Requires More Memory!
No Multithreading
Issues!
Can Take Advantage of
Multiple CPUs
CPU Scheduling
High IO
High CPU
http://www.cs.rutgers.edu/~pxk/416/notes/07-scheduling.html
High I/O
High IO
Fill these I/O blocks with other CPU tasks!
Multithreading ticker
t1 = Thread.new do	
puts "[Thread 1] Started"	
sleep 2	
puts "[Thread 1] Completed"	
end	
!
t2 = Thread.new do	
5.times do |n|	
puts "[Thread 2] tick #{n}"	
end	
end
Multithreading ticker
t1 = Thread.new do	
puts "[Thread 1] Started"	
sleep 2	
puts "[Thread 1] Completed"	
end	
!
t2 = Thread.new do	
5.times do |n|	
puts "[Thread 2] tick #{n}"	
end	
end
$ ruby thread_ticker.rb	

[Thread 1] Started	

[Thread 2] tick 0	

[Thread 2] tick 1	

[Thread 2] tick 2	

[Thread 2] tick 3	

[Thread 2] tick 4
Multithreading ticker
t1 = Thread.new do	
puts "[Thread 1] Started"	
sleep 2	
puts "[Thread 1] Completed"	
end	
!
t2 = Thread.new do	
5.times do |n|	
puts "[Thread 2] tick #{n}"	
end	
end
Where’s Completed ?
$ ruby thread_ticker.rb	

[Thread 1] Started	

[Thread 2] tick 0	

[Thread 2] tick 1	

[Thread 2] tick 2	

[Thread 2] tick 3	

[Thread 2] tick 4
Multithreading ticker
t1 = Thread.new do	
puts "[Thread 1] Started"	
sleep 2	
puts "[Thread 1] Completed"	
end	
!
t2 = Thread.new do	
5.times do |n|	
puts "[Thread 2] tick #{n}"	
end	
end	
!
t1.join	
t2.join
Multithreading ticker
t1 = Thread.new do	
puts "[Thread 1] Started"	
sleep 2	
puts "[Thread 1] Completed"	
end	
!
t2 = Thread.new do	
5.times do |n|	
puts "[Thread 2] tick #{n}"	
end	
end	
!
t1.join	
t2.join
$ ruby thread_ticker.rb	

[Thread 1] Started	

[Thread 2] tick 0	

[Thread 2] tick 1	

[Thread 2] tick 2	

[Thread 2] tick 3	

[Thread 2] tick 4	

[Thread 1] Completed
Issues with Threads
No control when Threads are preempted!
Deadlock!
Race conditions!
Hard to debug
Race Condition
Race Condition
Race Condition
Fibers
Fibers
Programmers specify when to give up control!
Prevents concurrency issues!
Lightweight
Fibers vs Threads
http://oldmoe.blogspot.com/2008/08/ruby-fibers-vs-ruby-threads.html
Fibonacci
fib = Fiber.new do	
f1 = f2 = 1	
loop do	
Fiber.yield f1	
f1, f2 = f2, f1 + f2	
end	
end	
!
5.times { p fib.resume }
$ ruby fiber_fib.rb	
1	
1	
2	
3	
5
Fiber Ticker
require 'fiber'	
!
fb1 = Fiber.new do	
puts "[Fiber 1] Started"	
sleep 2	
puts "[Fiber 1] Completed"	
end	
!
fb2 = Fiber.new do	
5.times do |n|	
puts "[Fiber 2] tick #{n}"	
end	
end	
!
fb1.resume	
fb2.resume
Fiber Ticker
require 'fiber'	
!
fb1 = Fiber.new do	
puts "[Fiber 1] Started"	
sleep 2	
puts "[Fiber 1] Completed"	
end	
!
fb2 = Fiber.new do	
5.times do |n|	
puts "[Fiber 2] tick #{n}"	
end	
end	
!
fb1.resume	
fb2.resume
$ ruby fiber_tick.rb	

[Fiber 1] Started	

[Fiber 1] Completed	

[Fiber 2] tick 0	

[Fiber 2] tick 1	

[Fiber 2] tick 2	

[Fiber 2] tick 3	

[Fiber 2] tick 4
http://schmurfy.github.io/2011/09/25/on_fibers_and_threads.html
Event Machine
https://github.com/eventmachine/eventmachine
Evented Ticker
require 'fiber'	
require 'eventmachine'	
!
EM::run do	
fb1 = Fiber.new do	
puts "[Fiber 1] Started"	
EM::add_timer(2){ fb1.resume }	
Fiber.yield	
puts "[Fiber 1] Completed"	
EM::stop()	
end	
!
fb2 = Fiber.new do	
5.times {|n| puts "[Fiber 2] tick #{n}" }	
end	
!
fb1.resume	
fb2.resume	
end
$ ruby evented_ticker.rb	

[Fiber 1] Started	

[Fiber 2] tick 0	

[Fiber 2] tick 1	

[Fiber 2] tick 2	

[Fiber 2] tick 3	

[Fiber 2] tick 4	

[Fiber 1] Completed
When does
Multithreading help?
High I/O time such as
File I/O DB call API request
Demo
https://github.com/sudizhe/parallel_programming_demo
Fibers
Programmers specify when to give up control!
Prevents concurrency issues!
Lightweight
Multiprocessing?
http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
Multiprocessing?
http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
Global Interpreter Lock (GIL)
Multiprocessing?
http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
Global Interpreter Lock (GIL)
Rails Deploy
Spawning Processes
Parallel Gem
inDinero Enterprise
inDinero Enterprise
150,000 Transactions
inDinero Enterprise
150,000 Transactions x 15,000 Rules
Multithread Your Application
Recap
Multithreading
Thread Class
Fibers
Event Machine
Multiprocessing
Process Class
Unicorn Magic
Hadoop
Q & A

More Related Content

KEY
Ruby 1.9 Fibers
PPTX
Fiber in the 10th year
PDF
Ruby's Concurrency Management: Now and Future
PDF
How does Scala's Future work?
KEY
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
KEY
Actors and Threads
KEY
Hybrid concurrency patterns
PPTX
One Container, Two Container, Three Containers, Four
Ruby 1.9 Fibers
Fiber in the 10th year
Ruby's Concurrency Management: Now and Future
How does Scala's Future work?
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Actors and Threads
Hybrid concurrency patterns
One Container, Two Container, Three Containers, Four

Similar to Multithread Your Application (20)

PDF
Fiber in the 10th year
KEY
Some Rough Fibrous Material
PPT
Testing multithreaded java applications for synchronization problems
KEY
Asynchronous Awesome
PDF
Bringing Concurrency to Ruby - RubyConf India 2014
PDF
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
PDF
Upgrading to Rails 3
PDF
Lessons Learnt in 2009
KEY
Ruby Concurrency and EventMachine
PDF
Apache Kafka – (Pattern and) Anti-Pattern
PDF
Learning Python through Minecraft on the Raspberry Pi - Worksheets
PDF
Looming Marvelous - Virtual Threads in Java Javaland.pdf
PPTX
Repeating History...On Purpose...with Elixir
PDF
Infrastructure as code might be literally impossible part 2
PDF
Docker
PDF
Rocket Fuelled Cucumbers
PDF
GildaVM: a Non-Blocking I/O Architecture for the Cog VM
KEY
Ruby Concurrency Realities
PDF
Embracing Events
PPTX
Parallel batch processing with spring batch slideshare
Fiber in the 10th year
Some Rough Fibrous Material
Testing multithreaded java applications for synchronization problems
Asynchronous Awesome
Bringing Concurrency to Ruby - RubyConf India 2014
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Upgrading to Rails 3
Lessons Learnt in 2009
Ruby Concurrency and EventMachine
Apache Kafka – (Pattern and) Anti-Pattern
Learning Python through Minecraft on the Raspberry Pi - Worksheets
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Repeating History...On Purpose...with Elixir
Infrastructure as code might be literally impossible part 2
Docker
Rocket Fuelled Cucumbers
GildaVM: a Non-Blocking I/O Architecture for the Cog VM
Ruby Concurrency Realities
Embracing Events
Parallel batch processing with spring batch slideshare
Ad

Recently uploaded (20)

PPTX
PHIL.-ASTRONOMY-AND-NAVIGATION of ..pptx
PPTX
Self management and self evaluation presentation
PDF
Instagram's Product Secrets Unveiled with this PPT
PPTX
Relationship Management Presentation In Banking.pptx
PPTX
Impressionism_PostImpressionism_Presentation.pptx
PPTX
chapter8-180915055454bycuufucdghrwtrt.pptx
PPTX
The spiral of silence is a theory in communication and political science that...
PPTX
worship songs, in any order, compilation
PPTX
S. Anis Al Habsyi & Nada Shobah - Klasifikasi Hambatan Depresi.pptx
PPTX
2025-08-10 Joseph 02 (shared slides).pptx
PPTX
An Unlikely Response 08 10 2025.pptx
PPTX
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
PPTX
Tour Presentation Educational Activity.pptx
PPTX
AcademyNaturalLanguageProcessing-EN-ILT-M02-Introduction.pptx
PDF
Swiggy’s Playbook: UX, Logistics & Monetization
PPTX
Intro to ISO 9001 2015.pptx wareness raising
PDF
Presentation1 [Autosaved].pdf diagnosiss
PPTX
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
DOC
学位双硕士UTAS毕业证,墨尔本理工学院毕业证留学硕士毕业证
PPTX
Primary and secondary sources, and history
PHIL.-ASTRONOMY-AND-NAVIGATION of ..pptx
Self management and self evaluation presentation
Instagram's Product Secrets Unveiled with this PPT
Relationship Management Presentation In Banking.pptx
Impressionism_PostImpressionism_Presentation.pptx
chapter8-180915055454bycuufucdghrwtrt.pptx
The spiral of silence is a theory in communication and political science that...
worship songs, in any order, compilation
S. Anis Al Habsyi & Nada Shobah - Klasifikasi Hambatan Depresi.pptx
2025-08-10 Joseph 02 (shared slides).pptx
An Unlikely Response 08 10 2025.pptx
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
Tour Presentation Educational Activity.pptx
AcademyNaturalLanguageProcessing-EN-ILT-M02-Introduction.pptx
Swiggy’s Playbook: UX, Logistics & Monetization
Intro to ISO 9001 2015.pptx wareness raising
Presentation1 [Autosaved].pdf diagnosiss
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
学位双硕士UTAS毕业证,墨尔本理工学院毕业证留学硕士毕业证
Primary and secondary sources, and history
Ad

Multithread Your Application