On 18Aug2021 16:30, Paul Prescod email@example.com wrote:
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).
Do you mean a facility for setting an attribute on an object and reversing that when you exit the scope where you set it?
I've got a stackattrs context manager I use for that. Example use:
# this is on PyPI from cs.context import stackattrs
with stackattrs(getcontext(), prec=8): # .prec=8 vals = map(my_algorithm, range(0, 10)) # .prec is whatever it used to be, including absent
Otherwise it is not clear to me what you are after. If this isn't what you want, can you describe what's different?
Cheers, Cameron Simpson firstname.lastname@example.org