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

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. What does this program print? p Thread.new { 21 * 2 }.value

    Answer

    42

    value waits for the thread and returns what its block produced.

  7. 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.

  8. 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.2

    Answer

    true

    Four waits of fifty milliseconds overlap, so the wall time stays far under the quarter second of four sequential sleeps.

  9. 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.

  10. 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