Skip to content

Instantly share code, notes, and snippets.

@mnjul
Created May 27, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnjul/82151862f7c9585dcea616a7e2e82033 to your computer and use it in GitHub Desktop.
Save mnjul/82151862f7c9585dcea616a7e2e82033 to your computer and use it in GitHub Desktop.
Python Thread Local Storage "global instantiation" vs "thread-local accessing"
# python 2.7.6
import threading
import sys
import traceback
import random
# too lazy to properly turn off output buffering
def msg(msg):
sys.stderr.write(msg)
sys.stderr.flush()
class Storage(threading.local):
def __init__(self):
ident = threading.current_thread().ident
msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))
threading.local.__init__(self)
self.storage = {}
def get(self, k):
return self.storage.get(k)
def set(self, k, v):
self.storage[k] = v
storage = Storage()
class Runner(threading.Thread):
def run(self):
global storage
value = random.randint(1, 1000000)
ident = threading.current_thread().ident
msg('Thread %x: write %d\n' % (ident, value))
storage.set('keykey', value)
read_value = storage.get('keykey')
msg('Thread %x: read %d, correct=%s\n' % (ident, read_value, read_value == value))
threads = [Runner() for i in xrange(10)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
@mnjul
Copy link
Author

mnjul commented May 27, 2016

Example output:

Thread 7fff785a6000: init ->   File "py_thr_local.py", line 26, in <module>
    storage = Storage()
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000401000: write 424114
Thread 700000804000: write 559469
Thread 700000c07000: write 970899
Thread 70000100a000: write 577423
Thread 70000140d000: write 675304
Thread 700000c07000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000401000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000c07000: read 970899, correct=True
Thread 700000401000: read 424114, correct=True
Thread 700000804000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 70000140d000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700001810000: write 909920
Thread 700000804000: read 559469, correct=True
Thread 70000140d000: read 675304, correct=True
Thread 70000100a000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700001c13000: write 906230
Thread 70000100a000: read 577423, correct=True
Thread 700001810000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000401000: write 111907
Thread 700001810000: read 909920, correct=True
Thread 700001c13000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700001c13000: read 906230, correct=True
Thread 700000804000: write 809142
Thread 700000401000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000401000: read 111907, correct=True
Thread 700000804000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000804000: read 809142, correct=True
Thread 700000c07000: write 210861
Thread 700000c07000: init ->   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 783, in __bootstrap
    self.__bootstrap_inner()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "py_thr_local.py", line 36, in run
    storage.set('keykey', value)
  File "py_thr_local.py", line 14, in __init__
    msg('Thread %x: init -> %s\n' % (ident, ''.join(traceback.format_stack())))

Thread 700000c07000: read 210861, correct=True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment