<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged gil</title>
<link>https://coderlegion.com/tag/gil</link>
<description>Powered by Question2Answer</description>
<item>
<title>Testing the performance of Python with and without GIL</title>
<link>https://coderlegion.com/461/testing-the-performance-of-python-with-and-without-gil</link>
<description>&lt;p&gt;Since Python 3.13, the famous Python GIL is optional. This is a long requested functionality but, is the performance gain of threaded code real? &lt;/p&gt;
&lt;p&gt;Let's test the performance of a threaded application with and without the GIL.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=716923579239862278&quot; alt=&quot;Testing the performance of Python with and without GIL&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;what-is-gil-&quot;&gt;What is GIL?&lt;/h3&gt;
&lt;p&gt;GIL stands for Global Interpreter Lock, and is a mechanism only present in the standard Python implementation (CPython) that ensures the coherence and safety of Python internal and runtime objects. The GIL makes sure that only a single thread is executed at the same time, losing the benefits of a multicore environment.&lt;/p&gt;
&lt;p&gt;The new versions make this security mechanism entirely optional, opening  whole word of possible optimizations in multicore environments.&lt;/p&gt;
&lt;p&gt;NOTE: this functionality is not yet available in the official releases of Python 3.14, but it will in the future.&lt;/p&gt;
&lt;h3 id=&quot;installing-python-3-14&quot;&gt;Installing Python 3.14&lt;/h3&gt;
&lt;p&gt;Use these commands to install Python 3.14 in Ubuntu:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;add-apt-repository ppa:deadsnakes/ppa
apt install python3.14-full python3.14-nogil
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the installation, we can test the installation with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python3.14 --version
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Great, now we have a working Python 3.14 installation. Now, we can create a virtual environment with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python3.14 -m venv ~/venv314
source ~/venv314/bin/activate
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we are ready to execute our tests!&lt;/p&gt;
&lt;h3 id=&quot;testing-the-same-code-with-and-without-gil&quot;&gt;Testing the same code with and without GIL&lt;/h3&gt;
&lt;p&gt;We have coded this application:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import time
import threading
import math

def test_function(num_thread):
    thread_start_time = time.time()
    print(F&quot;Executing thread #{num_thread}...&quot;)
    math.factorial(250000)
    thread_execution_time = time.time() - thread_start_time
    print(F&quot;...thread #{num_thread} finalized in {thread_execution_time} seconds&quot;)

print(&quot;Initializing thread test...&quot;)
start_time = time.time()

# Create multiple threads
threads = []
for num_thread in range(5):
    thread = threading.Thread(target=test_function, args=(num_thread,))
    thread.start()
    threads.append(thread)

# Wait for all threads to finish
for thread in threads:
    thread.join()

execution_time = time.time() - start_time
print(f&quot;Finalized! Execution time: {execution_time} seconds&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is a really simple application that spawns some threads and performs a CPU intensive task in each thread (a factorial). Now, we can execute the test code in each version, with and without GIL:&lt;/p&gt;
&lt;p&gt;With GIL enabled:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;PYTHON_GIL=1 python3.14-nogil test.py

Initializing thread test...
Executing thread 0...
Executing thread 1...
Executing thread 2...
...thread 0 finalized in 1.6720194816589355 seconds
...thread 1 finalized in 1.6916308403015137 seconds
Executing thread 3...
...thread 2 finalized in 1.8445136547088623 seconds
Executing thread 4...
...thread 3 finalized in 0.9945917129516602 seconds
...thread 4 finalized in 1.0793228149414062 seconds
Finalized! Execution time: 4.599931955337524 seconds
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And without GIL:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;PYTHON_GIL=0 python3.14-nogil test.py

Initializing thread test...  
Executing thread 0...
Executing thread 1...
Executing thread 2...
Executing thread 3...
Executing thread 4...
...thread 0 finalized in 0.8833539485931396 seconds
...thread 2 finalized in 0.8942356109619141 seconds
...thread 3 finalized in 0.934593677520752 seconds
...thread 4 finalized in 1.2945129871368408 seconds
...thread 1 finalized in 1.2981431484222412 seconds
Finalized! Execution time: 1.3003475666046143 seconds
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wow! The performance gain is notable!&lt;/p&gt;
&lt;h3 id=&quot;a-brilliant-future&quot;&gt;A brilliant future&lt;/h3&gt;
&lt;p&gt;The possibility of running Python code without GIL with the default Python interpreter opens a lot of optimization opportunities to the Python ecosystem. We can expect an improved Django, a faster FastAPI, more optimized ASGI or WSGI servers...&lt;/p&gt;
&lt;p&gt;Let's wait for this future together!&lt;/p&gt;
&lt;h3 id=&quot;about-the-list&quot;&gt;About the list&lt;/h3&gt;
&lt;p&gt;Among the Python and Docker posts, I will also write about other related topics, like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Software architecture&lt;/li&gt;
&lt;li&gt;Programming environments&lt;/li&gt;
&lt;li&gt;Linux operating system&lt;/li&gt;
&lt;li&gt;Etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you found some interesting technology, programming language or whatever, please, let me know! I'm always open to learning something new!&lt;/p&gt;
&lt;h3 id=&quot;about-the-author&quot;&gt;About the author&lt;/h3&gt;
&lt;p&gt;I'm Andrés, a full-stack software developer based in Palma, on a personal journey to improve my coding skills. I'm also a self-published fantasy writer with four published novels to my name. Feel free to ask me anything!&lt;/p&gt;
</description>
<category>Getting Started</category>
<guid isPermaLink="true">https://coderlegion.com/461/testing-the-performance-of-python-with-and-without-gil</guid>
<pubDate>Mon, 25 Nov 2024 03:51:53 +0000</pubDate>
</item>
</channel>
</rss>