Your SlideShare is downloading. ×
Multithread Your Application
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Multithread Your Application

150
views

Published on

Rubyconfph 2014 talk on parallel programming. …

Rubyconfph 2014 talk on parallel programming.

Demo can be found at:

https://github.com/sudizhe/parallel_programming_demo


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
150
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Parallel Computing with ruby
  • 2. My Company
  • 3. My Company
  • 4. Challenges
  • 5. • Learning Ruby Challenges
  • 6. • Learning Ruby • Rails 2 to Rails 3 Challenges
  • 7. • Learning Ruby • Rails 2 to Rails 3 • Integrating with Banking APIs Challenges
  • 8. What is Parallel Computing?
  • 9. Multithreading! vs! Multiprocessing
  • 10. 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
  • 11. CPU Scheduling High IO High CPU http://www.cs.rutgers.edu/~pxk/416/notes/07-scheduling.html
  • 12. High I/O High IO Fill these I/O blocks with other CPU tasks!
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. Issues with Threads No control when Threads are preempted! Deadlock! Race conditions! Hard to debug
  • 19. Race Condition
  • 20. Race Condition
  • 21. Race Condition
  • 22. Fibers
  • 23. Fibers Programmers specify when to give up control! Prevents concurrency issues! Lightweight
  • 24. Fibers vs Threads http://oldmoe.blogspot.com/2008/08/ruby-fibers-vs-ruby-threads.html
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. Event Machine https://github.com/eventmachine/eventmachine
  • 29. 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
  • 30. When does Multithreading help? High I/O time such as File I/O DB call API request
  • 31. Demo https://github.com/sudizhe/parallel_programming_demo
  • 32. Fibers Programmers specify when to give up control! Prevents concurrency issues! Lightweight
  • 33. Multiprocessing? http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
  • 34. Multiprocessing? http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ Global Interpreter Lock (GIL)
  • 35. Multiprocessing? http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ Global Interpreter Lock (GIL)
  • 36. Rails Deploy
  • 37. Spawning Processes
  • 38. Parallel Gem
  • 39. inDinero Enterprise
  • 40. inDinero Enterprise 150,000 Transactions
  • 41. inDinero Enterprise 150,000 Transactions x 15,000 Rules
  • 42. Recap Multithreading Thread Class Fibers Event Machine Multiprocessing Process Class Unicorn Magic Hadoop
  • 43. Q & A