Created
September 22, 2018 07:48
-
-
Save hellysmile/821f9ec110cff9140e52baabe6cb6f04 to your computer and use it in GitHub Desktop.
threading_local.py
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
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