Urgent.News

600+ sources. One page. See who else covered it.

Editions

Tech

The Python GIL, explained without the hand-waving

Every Python developer eventually hits this conversation: "Just use threads to speed it up." "Python threads don't run in parallel. The GIL." "So threads are useless in Python?" "No, they're great for—" "You just said they don't run in parallel!" Both people are half right. Here's the actual model, and the decision rules that follow from it. What the GIL actually is The Global Interpreter Lock is…

The Global Interpreter Lock (GIL) is a mutex in CPython that allows only one thread to execute Python bytecode at any given time. While multiple threads can exist, only the thread holding the GIL can advance Python code. The GIL is necessary for memory management due to CPython's reference counting system, which would be corrupted by concurrent incrementing/decrementing of object reference counters by multiple threads.

However, the GIL is released during blocking operations such as I/O, file reads, time.sleep(), database queries, and heavy C-extension work. Therefore, threads in Python are beneficial for I/O-bound tasks, not CPU-bound work. CPU-bound tasks see no performance gain from threading, while heavy numeric code using NumPy, Numba, or Cython can leverage parallelism within a single process due to the GIL being released during C loops.

For mixed workloads, one can combine processes for compute tasks and threads or asyncio for I/O-bound tasks. It's important to note that CPython, the default Python interpreter, still has the GIL, and there are no plans to remove it anytime soon.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Choosing colors (for developers)

The eleven-shade color scale from Tailwind CSS is one of those ideas I keep stealing. You know the one: 50 through 950 gives you enough stops for backgrounds, borders, hover states, text, everything.

  • Tailwind CSS offers an eleven-shade color scale for design consistency.
  • Use OKLCH color model to derive shades and maintain consistent neutral palette with --gray- aliases.

More from Friday 14 August →