
Scenario and Use Case
I want to write a program that performs a CPU-intensive calculation for 10 million numbers multiple times. For proof of concept, I will have it run 25 times. That would be 10M * 25 = 250 million calculations.
In GIL-enabled Python, only one thread can execute Python bytecode at a time. Even if you create many threads, they take turns running the Python interpreter.
In GIL-free Python (3.14 free-threaded build in this blog post), multiple threads can execute Python code simultaneously, making true parallelism possible—especially on multi-core CPUs.
In the program listed below, each thread can run Python bytecode simultaneously on different CPU cores. CPU-heavy tasks will scale with the number of cores in the computer.
Python 3.14 and threading
I will use Python 3.14 GIL-free with multithreading to run the CPU-intensive computations.
By default, Python pre-3.14 runs with GIL-enabled, so that is not true concurrency, even when using the multithreading module.
Python code to make 25 multithreaded requests
Here's a Python program that creates 25 threads, each of which performs on 10 million computations. This program uses the Python multithreading module and simultaneously uses multiple CPU cores.
#!/usr/bin/env python3
#
# Python 3.14 GIL-free specific multithreading program to use 25 concurrent connections
# Each connection/thread performs a CPU-intensive task --
# iterate through 10 million numbers and return a simple calculation.
# This would have been limited by the GIL in previous versions,
# but now can run in parallel across multiple threads without blocking each other.
#
# Author : Arul John
# Created : 2025-04-22
#
import sys
import threading
import time
# Vars
THREADS = 25
# A CPU-bound task that previously would have been limited by the GIL
def heavy_calculation(i, n):
result = 0
for j in range(n):
result += j * 2
print(f'🔥 THREAD {i} 🔥 result = {result}')
return result
# Create and run multiple threads in parallel to perform the heavy calculation
def run_parallel_threads():
threads = []
for i in range(THREADS):
t = threading.Thread(target=heavy_calculation, args=(i, 10_000_000,))
threads.append(t)
t.start()
print(f'Started thread # {i}')
for t in threads:
t.join()
# Main
if __name__ == "__main__":
start = time.perf_counter()
run_parallel_threads()
print(f"🪨 GIL enabled: {sys._is_gil_enabled()}")
print(f"🪨 Completed in {time.perf_counter() - start:.2f} seconds")
Conclusion
This is a sample Python program that uses the multithreading module in the way it is meant to be used with computers running multiple cores.
If you have any other way of making concurrent calls using multithreading, let me know. Thanks for reading.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.