Python Forum

Full Version: Python [nogil]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Today I found this: https://github.com/colesbury/nogil

Quote:Overview

This is a proof-of-concept implementation of CPython that supports multithreading without the global interpreter lock (GIL). An overview of the design is described in the Python Multithreading without GIL Google doc.


I just used pyenv to install it:
pyenv install nogil-3.9.10-1
Good luck, I ain't going to need it!
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.
It's indeed an interesting step of the Python developers. However, I think it is important to mention the the GIL-free Python is not ABI compatible with the GIL Python and, when writting C extensions, the programmer may need to take care of the locking when necessary.

This is probably not too much of a downside for daily "bread and butter" use, but it will interesting to see how well the new feature will be excepted and used.

Regards, noisefloor