Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Threads and Processes (Lightning Talk Given at Rails Israel 2015)

7b5a451ee25044b9c869e3e98b79425d?s=47 Ariel Caplan
November 24, 2015

Threads and Processes (Lightning Talk Given at Rails Israel 2015)

Many of us use threads and/or processes in our code, whether explicitly or implicitly. But do we really understand what threads and processes are? Can we clearly evaluate the pros and cons of each? We will attempt to clarify these concepts in a simple, concise fashion.

7b5a451ee25044b9c869e3e98b79425d?s=128

Ariel Caplan

November 24, 2015
Tweet

More Decks by Ariel Caplan

Other Decks in Technology

Transcript

  1. Threads and Processes Ariel Caplan

  2. Concurrency <= Parallelism

  3. Threads && Processes Both provide concurrent execution of code! Neither

    guarantee execution order! Both add overhead to your code!
  4. Threads vs. Processes Process Process Memory Memory Thread Thread Memory

  5. Threads vs. Processes @var @var @var = 1 @var =

    1 @var @var @var = 1
  6. Threads vs. Processes @var @var @var = 1 @var =

    1 @var @var @var = 2
  7. Threads vs. Processes @var @var @var = 1 @var @var

    @var = 2 @var = 2
  8. Threads vs. Processes @var @var @var = 1 @var @var

    @var = 2 @var = 2 WARNING:
 OVERSIMPLIFICATION
  9. Threads vs. Processes Threads share memory and state! Processes share

    nothing
  10. Threads vs. Processes in MRI Threads share memory and state!

    Processes share nothing! Threads are concurrent, not parallel, due to the GIL 
 (Global Interpreter Lock)! Processes operate in true parallel
  11. Parallelism Concurrency Processes Multi-Core System Single-Core System Threads JRuby, Rubinius,

    Nonblocking IO Standard Calculations in MRI The Bottom Line
  12. Implication: Sidekiq Concurrency For a server running only Sidekiq on

    MRI:! 1 Sidekiq process per core! Concurrency (number of threads) = 1 / CPU Time! Example: 8-core machine with 75% IO, 25% CPU:! 8 Sidekiq processes! Sidekiq concurrency of 1/(25%) = 4
  13. Thank You For Listening! amcaplan.ninja! @amcaplan! ↑! Questions