Skip to main content

Command Palette

Search for a command to run...

Multi-tasking: Python vs Other Languages

In this article we will understand about CPython, GIL and how to achieve efficiency in I/O Bound and CPU-Intensive tasks. After that we will briefly look at other python implementations and how many of them can achieve true CPU parallelism. Finally, we will compare python with other languages.

Updated
•4 min read•View as Markdown
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

What is CPython?

CPython is the default, official, and most widely used implementation of the Python programming language. When you download Python from the official Python Website, you are downloading CPython. It is called "CPython" because its core engine is written in C.

How CPython Works

CPython acts as both a compiler and an interpreter to translate your human-readable code into something your computer hardware understands:

  1. Compilation: It automatically compiles your Python source code (.py) into a lower-level intermediate format called bytecode.

  2. Interpretation: It feeds that bytecode into the Python Virtual Machine (PVM), an internal interpreter that executes the instructions line-by-line.

Python Code (.py)
        ↓
     Compiler
        ↓
Python Bytecode (.pyc)
        ↓
Python Virtual Machine
        ↓
      Output

Read More on Python vs. CPython on Stack Overflow.


Global Interpreter Lock (GIL)

CPython has a feature called the Global Interpreter Lock (GIL).

The GIL allows only one thread at a time to execute Python bytecode within a process.

Because of the GIL, multiple python threads can make progress concurrently but they can not achieve true CPU parallelism in CPython.

Example:

Thread A
Thread B

On an n-core machine, these two threads will generally take turns (on a single core) holding the GIL, rather than running Python bytecode simultaneously on different cores.

Core 1: Thread A runs
Core 2: idle
Core 3: idle
.
.
.
Core n: idle

Thread A releases GIL

Core 1: Thread B runs
Core 2: idle
Core 3: idle
.
.
.
Core n: idle

The threads are concurrent (both make progress over time) but not truly parallel.

That's why for CPU-intensive work in CPython, we often use multiprocessing instead of threading, because each process gets its own Python interpreter and its own GIL.

When people say:

"Python threads don't run in parallel"

they are specifically talking about CPython.

In one sentence: CPython is the standard C-based Python interpreter, and its GIL is the main reason Python threads are great for I/O concurrency but not for CPU parallelism.

Scenario How to achieve efficiency?
I/O Bound tasks multi-threading Concurrency (asynchronous code)
CPU-Intensive tasks multi-processing True Parallelism

Other Python implementations and thread parallelism

Implementation Written In Has GIL? True CPU Parallelism with Threads?
CPython C Yes ❌ No
PyPy Python + RPython Yes ❌ No
Jython Java No JVM GIL ✅ Yes
IronPython C#/.NET No .NET GIL ✅ Yes
MicroPython C Limited ❌ No

What about other languages?

If your machine has n cores:

Languages that can achieve thread-based parallelism

✅ C
✅ C++
✅ Java
✅ C#
✅ Rust
✅ Go

Example:

Core 1 → Thread A
Core 2 → Thread B
Core 3 → Thread C
Core 4 → Thread D

These languages do not have a CPython-style GIL preventing CPU parallelism.

What about Node.js?

Node.js is a little different.

The main JavaScript thread is single-threaded. However, Node.js can still utilize multiple cores via:

  • Worker Threads

  • Child Processes

  • Cluster module

  • libuv thread pool (for some operations)

So Node.js can achieve parallelism, but not by running ordinary JavaScript code on the main event-loop thread.


Thank you for your time! 😊

Connect with me on LinkedIn