Threads in both
Four jobs that wait
Threads pay off when the work waits: a network response, a disk read, a sleep. Four jobs that each wait 0.2 seconds should finish in about 0.2 seconds when they run at the same time, in either language, because the waiting does not need the interpreter.
Ruby
threads = 4.times.map do Thread.new { sleep 0.2 } end threads.each(&:join) puts "4 threads, 0.2s of sleep each: done"
time ruby threads.rb
4 threads, 0.2s of sleep each: done
Python
import threading import time def work(): time.sleep(0.2) threads = [threading.Thread(target=work) for _ in range(4)] for thread in threads: thread.start() for thread in threads: thread.join() print("4 threads, 0.2s of sleep each: done")
time python3 threads.py
4 threads, 0.2s of sleep each: done
Both runs take about a quarter of a second: the four waits overlap. On the machine this course was checked with, time printed real 0m0.318s for the Ruby run and real 0m0.239s for the Python one. The total wall time is close to one wait, not four, which is the whole point of threads for I/O.
What differs
- The lock has a different name. CRuby runs Ruby code under the Global VM Lock (GVL); CPython under the Global Interpreter Lock (GIL). Neither runs two threads of pure computation at the same moment, so both scale on waiting, not on arithmetic.
- The API. Thread.new { ... } returns a thread that starts at once; threading.Thread needs a start() call. Both wait with join, and both ship a queue for passing work.
- Processes. For CPU work both languages reach for processes: Process.fork on Linux, multiprocessing in Python. Lesson 9.5 goes through the Ruby side.
- The numbers. The timings above are one run on one machine; a hundredth of a second between them says nothing about the languages.
What to watch for
A thread that raises an exception can end silently unless the program
joins it or sets abort_on_exception (Ruby) or
reads thread.exc_info (Python). Join every thread
you start, in both languages.
Do not reach for threads because they are there. A script that runs four
commands one after another is often clearer than a pool, and a pool that
writes to one file needs a lock in either language.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What do the Global VM Lock and the Global Interpreter Lock have in common?
Answer
Both keep one thread of pure computation running at a time, so threads scale on waiting rather than on arithmetic
The lock in each language is why the two lessons reach for processes when the work is CPU-bound.
-
What does this program print?
t = Thread.new { sleep 0.01 } t.join p t.alive?Answer
false
After join the block has finished, so the thread is no longer running.
-
What does this program print?
p 4.times.map { |i| i * i }Answer
[0, 1, 4, 9]
times gives 0 to 3 and the block squares each number, which is the shape of the four-job loop.
-
What does this program print?
threads = 4.times.map { Thread.new { sleep 0.01 } } threads.each(&:join) puts "done"Answer
done
Every thread is joined before the line runs, so the program waits for all four and prints once.
-
Why does a CPU-bound job use processes rather than threads in Ruby?
Answer
The lock allows only one thread of Ruby code at a time, so processes are what use several cores
The same reasoning appears in the Python lesson, where multiprocessing is the answer to the GIL.
-
What does this program print?
p Thread.new { 21 * 2 }.valueAnswer
42
value waits for the thread and returns what its block produced.
-
What must a program do with every thread it starts?
Answer
Join it, or read its result, or a failure can disappear
The join is where a raised exception resurfaces, and both languages need the same care.
-
What does this program print?
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) threads = 4.times.map { Thread.new { sleep 0.05 } } threads.each(&:join) elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start p elapsed < 0.2Answer
true
Four waits of fifty milliseconds overlap, so the wall time stays far under the quarter second of four sequential sleeps.
-
What is the API difference between the two thread starters?
Answer
Thread.new starts the thread at once, threading.Thread needs a start() call
The Ruby block runs as soon as the thread exists, and the Python object waits for its start method.
-
What does this program print?
t = Thread.new { 1 } p t.join.equal?(t)Answer
true
join returns the thread it waited for, so the identity check passes.
Article author: Arthur Isaev