Python GIL affect

==IN PROGRESS== This article is in progress

Global Interpreter Lock
I’ve been trying to understand GIL by going through CPython source code and listing all the check points here..

This is mainly about these scenarios…

  1. How and why GIL has been injected in python.
  2. Where and when it effects the efficiency of a Python programme.
  3. Why and How threads becoming inefficient.
  4. Efficient way of writing parallel executions by considering GIL.
  5. Improvements made from Python 2.x to Python 3.x.

==IN PROGRESS== This article is in progress

Benchmark programme ..

import sys
import time
from threading import Thread

    xrange = range

LOOP_COUNT = 2*10**7

# Target function
def loop_it(n):
    for i in xrange(n):

# Sequetial execution of the loop
seq_exe_start = time.time()
print('Time taken for sequential run : ' + str(time.time() - seq_exe_start))

# Parallel execution of the loop with two threads each with
# half of the loop count of sequential run.

thread_pool = []

    thread_count = int(sys.argv[1])
except IndexError:
    thread_count = 1

for i in xrange(thread_count):
    thread_pool.append(Thread(target=loop_it, args=(LOOP_COUNT//thread_count, )))

start_time = time.time()
for i in thread_pool:
for i in thread_pool:

print('Time taken for parallel run with %s threads : %s ' %(
    str(thread_count), str(time.time() - start_time)))

[gsb@gsb python_thread_efficency]$ python 2
Time taken for sequential run : 0.364485025406
Time taken for parallel run with 2 threads : 0.466058969498 
Time taken for parallel run with 3 threads : 0.536095142365
Time taken for parallel run with 4 threads : 0.551960229874 ==IN PROGRESS==

Published: July 02 2015

  • category:
blog comments powered by Disqus