Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions python-gil/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# What Is the Python Global Interpreter Lock (GIL)?

This folder provides the code examples for the Real Python tutorial [What Is the Python Global Interpreter Lock (GIL)?](https://realpython.com/python-gil/).

The tutorial shows some of its examples at the REPL. Those are provided here as
runnable scripts that print the values the tutorial discusses:

| Script | Tutorial section |
| --- | --- |
| `reference_counting.py` | What Problem Did the GIL Solve for Python? |
| `single_threaded.py` | The Impact on Multi-Threaded Python Programs |
| `multi_threaded.py` | The Impact on Multi-Threaded Python Programs |
| `switch_interval.py` | Why Wasn't It Removed in Python 3? |
| `multiprocess.py` | How to Deal With Python's GIL |

## Running the Examples

The examples only use the Python standard library, so there's nothing to
install. Create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/),
then run any of the scripts:

```sh
$ python -m venv venv/
$ source venv/bin/activate
(venv) $ python single_threaded.py
```

The timings printed by `single_threaded.py`, `multi_threaded.py`, and
`multiprocess.py` depend on your machine, so they won't match the numbers in
the tutorial exactly. What matters is how they compare to each other.
22 changes: 22 additions & 0 deletions python-gil/multi_threaded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import time
from threading import Thread

COUNT = 50000000


def countdown(n):
while n > 0:
n -= 1


t1 = Thread(target=countdown, args=(COUNT // 2,))
t2 = Thread(target=countdown, args=(COUNT // 2,))

start = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
end = time.time()

print("Time taken in seconds -", end - start)
20 changes: 20 additions & 0 deletions python-gil/multiprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from multiprocessing import Pool
import time

COUNT = 50000000


def countdown(n):
while n > 0:
n -= 1


if __name__ == "__main__":
pool = Pool(processes=2)
start = time.time()
r1 = pool.apply_async(countdown, [COUNT // 2])
r2 = pool.apply_async(countdown, [COUNT // 2])
pool.close()
pool.join()
end = time.time()
print("Time taken in seconds -", end - start)
5 changes: 5 additions & 0 deletions python-gil/reference_counting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

a = []
b = a
print(sys.getrefcount(a))
15 changes: 15 additions & 0 deletions python-gil/single_threaded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import time

COUNT = 50000000


def countdown(n):
while n > 0:
n -= 1


start = time.time()
countdown(COUNT)
end = time.time()

print("Time taken in seconds -", end - start)
4 changes: 4 additions & 0 deletions python-gil/switch_interval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys

# The switch interval is set to 5 milliseconds:
print(sys.getswitchinterval())
Loading