Maintaining Dictionary Integrity While Running it Through Multithread Process
<p>I sped up a process by using a multithread function, however I need to maintain a relationship between the output and input. </p>

<pre><code>import requests
import pprint
import threading

ticker = ['aapl', 'googl', 'nvda']
url_array = []

for i in ticker:
    url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/' + i + '?formatted=true&amp;crumb=8ldhetOu7RJ&amp;lang=en-US&amp;region=US&amp;modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&amp;corsDomain=finance.yahoo.com'
    url_array.append(url)


def fetch_ev(url):
    urlHandler = requests.get(url)
    data = urlHandler.json()
    ev_single = data['quoteSummary']['result'][0]['defaultKeyStatistics']['enterpriseValue']['raw']
    ev_array.append(ev_single)  # makes array of enterprise values

threads = [threading.Thread(target=fetch_ev, args=(url,)) for url in
           url_array]  # calls multi thread that pulls enterprise value


for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

pprint.pprint(dict(zip(ticker, ev_array)))
</code></pre>

<p>Sample output of the code: </p>

<p>1) <code>{'aapl': '30.34B', 'googl': '484.66B', 'nvda': '602.66B'}</code></p>

<p>2) <code>{'aapl': '484.66B', 'googl': '30.34B', 'nvda': '602.66B'}</code></p>

<p>I need the value to be matched up with the correct ticker. </p>

<p>Edit: I know dictionaries do not preserve order. Sorry, perhaps I was a little (very) unclear in my question. I have an array of ticker symbols, that matches the order of my url inputs.  After running <code>fetch_ev</code>, I want to combine these ticker symbols with the matching enterprise value or <code>ev_single</code>. The order that they are stored in does not matter, however the pairings (<em>k</em> <em>v</em> pairs) or which values are stored with which ticker is <em>very</em> important. </p>

<p>Edit2 (MCVE) I changed the code to a simpler version of what I had- that shows the problem better. Sorry it's a little more complicated than I would have wanted complicated.</p>

<p>To make it easy to maintain the correspondence between input and output, the <code>ev_array</code> can be preallocated so it's the same size as the <code>ticker</code> array, and the <code>fetch_ev()</code> thread function can be given an extra argument specifying the index of the location in that array to store the value fetched.</p>

<p>The maintain the integrity of the <code>ev_array</code>, a <code>threading.RLock</code> was added to prevent concurrent access to the shared resource which might otherwise be written to simultaneously by more than one thread. (Since its contents are now referenced directly through the index passed to <code>fetch_ev()</code>, this may not be strictly necessary.)</p>

<p>I don't know the proper ticker â enterprise value concurrence to be able to verify the results that doing this produces:</p>

<pre><code>{'aapl': 602658308096L, 'googl': 484659986432L, 'nvda': 30338199552L}
</code></pre>

<p>but at least they're now the same each time it's run.</p>

<pre><code>import requests
import pprint
import threading

def fetch_ev(index, url):  # index parameter added
    response = requests.get(url)
    response.raise_for_status()
    data = response.json()
    ev_single = data['quoteSummary']['result'][0][
                     'defaultKeyStatistics']['enterpriseValue']['raw']
    with ev_array_lock:
        ev_array[index] = ev_single  # store enterprise value obtained

tickers = ['aapl', 'googl', 'nvda']
ev_array = [None] * len(tickers)  # preallocate to hold results
ev_array_lock = threading.RLock()  # to synchronize concurrent array access
urls = ['https://query2.finance.yahoo.com/v10/finance/quoteSummary/{}'
        '?formatted=true&amp;crumb=8ldhetOu7RJ&amp;lang=en-US&amp;region=US'
        '&amp;modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents'
        '&amp;corsDomain=finance.yahoo.com'.format(symbol)
           for symbol in tickers]
threads = [threading.Thread(target=fetch_ev, args=(i, url))
                for i, url in enumerate(urls)]  # activities to obtain ev's

for thread in threads:
    thread.start()
for thread in threads:
    thread.join()
pprint.pprint(dict(zip(tickers, ev_array)))
</code></pre>

python multithreading dictionary