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]
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/LKZRC7OK7GSBYWLGO2ZOCSBM3C2G3WEW/
Code of Conduct: http://python.org/psf/codeofconduct/