SlideShare a Scribd company logo
1 of 44
Download to read offline
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
Recap
Multithreading
Thread Class
Fibers
Event Machine
Multiprocessing
Process Class
Unicorn Magic
Hadoop
Q & A

More Related Content

Similar to Multithread Your Application

Fiber in the 10th year
Fiber in the 10th yearFiber in the 10th year
Fiber in the 10th yearKoichi Sasada
 
Some Rough Fibrous Material
Some Rough Fibrous MaterialSome Rough Fibrous Material
Some Rough Fibrous MaterialMurray Steele
 
Testing multithreaded java applications for synchronization problems
Testing multithreaded java applications for synchronization problemsTesting multithreaded java applications for synchronization problems
Testing multithreaded java applications for synchronization problemsVassil Popovski
 
Asynchronous Awesome
Asynchronous AwesomeAsynchronous Awesome
Asynchronous AwesomeFlip Sasser
 
Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Charles Nutter
 
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Paolo Negri
 
Upgrading to Rails 3
Upgrading to Rails 3Upgrading to Rails 3
Upgrading to Rails 3juliangiuca
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009pratiknaik
 
Ruby Concurrency and EventMachine
Ruby Concurrency and EventMachineRuby Concurrency and EventMachine
Ruby Concurrency and EventMachineChristopher Spring
 
Apache Kafka – (Pattern and) Anti-Pattern
Apache Kafka – (Pattern and) Anti-PatternApache Kafka – (Pattern and) Anti-Pattern
Apache Kafka – (Pattern and) Anti-Patternconfluent
 
Learning Python through Minecraft on the Raspberry Pi - Worksheets
Learning Python through Minecraft on the Raspberry Pi - WorksheetsLearning Python through Minecraft on the Raspberry Pi - Worksheets
Learning Python through Minecraft on the Raspberry Pi - WorksheetsManchesterBudo
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfjexp
 
Repeating History...On Purpose...with Elixir
Repeating History...On Purpose...with ElixirRepeating History...On Purpose...with Elixir
Repeating History...On Purpose...with ElixirBarry Jones
 
Infrastructure as code might be literally impossible part 2
Infrastructure as code might be literally impossible part 2Infrastructure as code might be literally impossible part 2
Infrastructure as code might be literally impossible part 2ice799
 
Rocket Fuelled Cucumbers
Rocket Fuelled CucumbersRocket Fuelled Cucumbers
Rocket Fuelled CucumbersJoseph Wilk
 
GildaVM: a Non-Blocking I/O Architecture for the Cog VM
GildaVM: a Non-Blocking I/O Architecture for the Cog VMGildaVM: a Non-Blocking I/O Architecture for the Cog VM
GildaVM: a Non-Blocking I/O Architecture for the Cog VMESUG
 
Ruby Concurrency Realities
Ruby Concurrency RealitiesRuby Concurrency Realities
Ruby Concurrency RealitiesMike Subelsky
 
Parallel batch processing with spring batch slideshare
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshareMorten Andersen-Gott
 

Similar to Multithread Your Application (20)

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

Recently uploaded

SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfSOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfMahamudul Hasan
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...amilabibi1
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lodhisaajjda
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxraffaeleoman
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar TrainingKylaCullinane
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoKayode Fayemi
 
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...David Celestin
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalFabian de Rijk
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaKayode Fayemi
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfSenaatti-kiinteistöt
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Baileyhlharris
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIINhPhngng3
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfSkillCertProExams
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatmentnswingard
 

Recently uploaded (15)

SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfSOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
 
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of Drupal
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
ICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdfICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdf
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 

Multithread Your Application