Created
May 19, 2017 19:47
-
-
Save leonsas/db68f96df896c78f1d3810e068148d8c to your computer and use it in GitHub Desktop.
Grokking the GIL and atomic ops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tweaked from https://emptysqua.re/blog/grok-the-gil-fast-thread-safe-python/ | |
import threading | |
for i in range(100): | |
n = 0 | |
def foo(): | |
global n | |
n += 1 | |
threads = [] | |
for i in range(1000): | |
t = threading.Thread(target=foo) | |
threads.append(t) | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() | |
print(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment