Let's imagine I have an algorithm which depends on a context variable.
I write an algorithm (elided below for space) which depends on it. Then I realize that I can improve the performance of my algorithm by using concurrent.futures. But my algorithm will change its behaviour because it does not inherit the context. Simply trying to parallelize an "embarrassingly parallel" algorithm changes its behaviour.
What I really want is a stack-scoped variable: a variable which retains its value for all child scopes whether in the same thread or not, unless it is overwritten in a child scope (whether in the same thread or not).
from decimal import getcontext import concurrent.futures
def my_algorithm(input): # some real algorithm here, which relies on decimal precision return getcontext().prec
getcontext().prec = 8 vals = map(my_algorithm, range(0, 10)) print(list(vals))
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: # Start the load operations and mark each future with its URL results = executor.map(my_algorithm, range(0, 10))
print(list(results))
Results:
[8, 8, 8, 8, 8, 8, 8, 8, 8, 8] [28, 28, 28, 28, 28, 28, 28, 28, 28, 28]