import time
from threading import Thread
ACTIVE = True
def spinner():
while ACTIVE:
pass
def main(threads):
print("Starting 16 CPU bound Threads")
for thread in threads:
thread.start()
while True:
time.sleep(0.01)
print(f"[{time.time():010.6f}] Main thread is still alive!")
if __name__ == "__main__":
try:
threads = [Thread(target=spinner) for _ in range(16)]
main(threads)
except KeyboardInterrupt:
print("Stopping threads")
ACTIVE = False
# waiting until all threads are stopped
while threads:
thread = threads.pop(0)
if thread.is_alive():
threads.append(thread)
Output:
[andre@andre-Fujitsu-i5 ~]$ pyenv shell nogil-3.9.10-1
[andre@andre-Fujitsu-i5 ~]$ python gil_killer.py
Starting 16 CPU bound Threads
[1674552711.670128] Main thread is still alive!
[1674552711.680244] Main thread is still alive!
[1674552711.690349] Main thread is still alive!
[1674552711.700457] Main thread is still alive!
[1674552711.710561] Main thread is still alive!
[1674552711.720664] Main thread is still alive!
[1674552711.730776] Main thread is still alive!
[1674552711.740888] Main thread is still alive!
[1674552711.750975] Main thread is still alive!
[1674552711.761084] Main thread is still alive!
[1674552711.771188] Main thread is still alive!
[1674552711.781366] Main thread is still alive!
[1674552711.793181] Main thread is still alive!
[1674552711.803325] Main thread is still alive!
[1674552711.819764] Main thread is still alive!
[1674552711.829890] Main thread is still alive!
[1674552711.840017] Main thread is still alive!
[1674552711.853108] Main thread is still alive!
[1674552711.863225] Main thread is still alive!
[1674552711.873339] Main thread is still alive!
[1674552711.883447] Main thread is still alive!
[1674552711.893637] Main thread is still alive!
[1674552711.903755] Main thread is still alive!
[1674552711.913868] Main thread is still alive!
[1674552711.923988] Main thread is still alive!
[1674552711.934101] Main thread is still alive!
[1674552711.948451] Main thread is still alive!
[1674552711.958573] Main thread is still alive!
[1674552711.968680] Main thread is still alive!
[1674552711.982994] Main thread is still alive!
[1674552711.993093] Main thread is still alive!
[1674552712.003201] Main thread is still alive!
[1674552712.016430] Main thread is still alive!
[1674552712.026542] Main thread is still alive!
[1674552712.036663] Main thread is still alive!
[1674552712.046769] Main thread is still alive!
[1674552712.061285] Main thread is still alive!
[1674552712.071407] Main thread is still alive!
[1674552712.081544] Main thread is still alive!
[1674552712.095864] Main thread is still alive!
[1674552712.105984] Main thread is still alive!
[1674552712.116100] Main thread is still alive!
[1674552712.126222] Main thread is still alive!
[1674552712.136353] Main thread is still alive!
^CStopping threads
Output:
[andre@andre-Fujitsu-i5 ~]$ pyenv shell 3.11.1
[andre@andre-Fujitsu-i5 ~]$ python gil_killer.py
Starting 16 CPU bound Threads
[1674552799.126536] Main thread is still alive!
[1674552799.729845] Main thread is still alive!
[1674552799.785723] Main thread is still alive!
[1674552799.866864] Main thread is still alive!
^CStopping threads
I'm impressed about the result. I know that locking is expensive, and many people tried to get rid of the GIL and the outcome was, that the performance was bad.
The plans are that Python 3.12 may exist in two different versions. One with GIL and a NOGIL version.