Skip to content

Instantly share code, notes, and snippets.

@hellysmile
Created September 22, 2018 07:48
Show Gist options
  • Save hellysmile/821f9ec110cff9140e52baabe6cb6f04 to your computer and use it in GitHub Desktop.
Save hellysmile/821f9ec110cff9140e52baabe6cb6f04 to your computer and use it in GitHub Desktop.
threading_local.py
from concurrent.futures import ThreadPoolExecutor
from threading import local, get_ident
ctx = local()
ctx.counter = 0
def inc(_):
try:
ctx.counter += 1
except AttributeError:
ctx.counter = 0
print(get_ident(), ctx.counter)
with ThreadPoolExecutor(max_workers=3) as executor:
list(executor.map(inc, list(range(100))))
print('MainThread:', ctx.counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment