44
Parallel Computing with ruby

Multithread Your Application

  • Upload
    andy-su

  • View
    189

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Multithread Your Application

Parallel Computing

with ruby

Page 2: Multithread Your Application

My Company

Page 3: Multithread Your Application

My Company

Page 4: Multithread Your Application

Challenges

Page 5: Multithread Your Application

• Learning Ruby

Challenges

Page 6: Multithread Your Application

• Learning Ruby

• Rails 2 to Rails 3

Challenges

Page 7: Multithread Your Application

• Learning Ruby

• Rails 2 to Rails 3

• Integrating with Banking APIs

Challenges

Page 8: Multithread Your Application

What is Parallel Computing?

Page 9: Multithread Your Application

Multithreading!vs!

Multiprocessing

Page 10: Multithread Your Application

Share Memory Space!

Lightweight!

May Have Memory Management Issues!

Can Sometimes Take Advantage of Multiple CPUs

Thread ProcessSeparate Memory Space!

Requires More Memory!

No Multithreading Issues!

Can Take Advantage of Multiple CPUs

Page 11: Multithread Your Application

CPU Scheduling

High IO

High CPU

http://www.cs.rutgers.edu/~pxk/416/notes/07-scheduling.html

Page 12: Multithread Your Application

High I/O

High IO

Fill these I/O blocks with other CPU tasks!

Page 13: Multithread Your Application

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}" endend

Page 14: Multithread Your Application

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}" endend

$ 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

Page 15: Multithread Your Application

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}" endend

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

Page 16: Multithread Your Application

Multithreading tickert1 = 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}" endend!t1.joint2.join

Page 17: Multithread Your Application

Multithreading tickert1 = 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}" endend!t1.joint2.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

Page 18: Multithread Your Application

Issues with Threads

No control when Threads are preempted!

Deadlock!

Race conditions!

Hard to debug

Page 19: Multithread Your Application

Race Condition

Page 20: Multithread Your Application

Race Condition

Page 21: Multithread Your Application

Race Condition

Page 22: Multithread Your Application

Fibers

Page 23: Multithread Your Application

Fibers

Programmers specify when to give up control!

Prevents concurrency issues!

Lightweight

Page 24: Multithread Your Application

Fibers vs Threads

http://oldmoe.blogspot.com/2008/08/ruby-fibers-vs-ruby-threads.html

Page 25: Multithread Your Application

Fibonacci

fib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend!5.times { p fib.resume }

$ ruby fiber_fib.rb11235

Page 26: Multithread Your Application

Fiber Tickerrequire '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}" endend!fb1.resumefb2.resume

Page 27: Multithread Your Application

Fiber Tickerrequire '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}" endend!fb1.resumefb2.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

Page 28: Multithread Your Application

Event Machine

https://github.com/eventmachine/eventmachine

Page 29: Multithread Your Application

Evented Tickerrequire '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.resumeend

$ 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

Page 30: Multithread Your Application

When does Multithreading help?

High I/O time such as

File I/O DB call API request

Page 31: Multithread Your Application

Demo

https://github.com/sudizhe/parallel_programming_demo

Page 32: Multithread Your Application

Fibers

Programmers specify when to give up control!

Prevents concurrency issues!

Lightweight

Page 33: Multithread Your Application

Multiprocessing?

http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/

Page 34: Multithread Your Application

Multiprocessing?

http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/

Global Interpreter Lock (GIL)

Page 35: Multithread Your Application

Multiprocessing?

http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/

Global Interpreter Lock (GIL)

Page 36: Multithread Your Application

Rails Deploy

Page 37: Multithread Your Application

Spawning Processes

Page 38: Multithread Your Application

Parallel Gem

Page 39: Multithread Your Application

inDinero Enterprise

Page 40: Multithread Your Application

inDinero Enterprise

150,000 Transactions

Page 41: Multithread Your Application

inDinero Enterprise

150,000 Transactions x 15,000 Rules

Page 42: Multithread Your Application
Page 43: Multithread Your Application

RecapMultithreading

Thread Class

Fibers

Event Machine

Multiprocessing

Process Class

Unicorn Magic

Hadoop

Page 44: Multithread Your Application

Q & A