How to collect function return values while multithreading without using globals?
<p>So I'm trying to work out a generic solution that will collect all values from a function and append them to a list that is later accessible.  This is to be used during <code>concurrent.futures</code> or <code>threading</code> type tasks.  Here is a solution I have using a global <code>master_list</code>:</p>

<pre><code>from concurrent.futures import ThreadPoolExecutor

master_list = []
def return_from_multithreaded(func):
    # master_list = []
    def wrapper(*args, **kwargs):
        # nonlocal master_list
        global master_list
        master_list += func(*args, **kwargs)
    return wrapper


@return_from_multithreaded
def f(n):
    return [n]


with ThreadPoolExecutor(max_workers=20) as exec:
    exec.map(f, range(1, 100))

print(master_list)
</code></pre>

<p>I would like to find a solution that does not include globals, and perhaps can return the commented out <code>master_list</code> that is stored as a closure?</p>