ThreadPoolExecutor is mixing the input data to the function on each thread? windows

The code that I present below (Python 3.6.12 -Windows x64) has a function for calculating the power flow in an electrical system which receives as a parameter the electrical power of a load and the network. Through the threads I intend to execute the function for different load values, however the answer is not correct because the data corresponding to certain values of P [0,10,20,30,40,50] are written in several results and not specifically where they correspond

    import concurrent.futures
    import pandapower as pp
    import pandapower.networks as pn
    import time
    
    net = pn.case14()
    param_list = [10,20,30,40,50]
    
    
    # Retrieve a single page and report the URL and contents
    
    def fdp(red,p_mw):
        red.load.p_mw.at[3] = p_mw
        pp.runopp(red)
        return red.res_bus.lam_p  
      
    print("Running threaded:")
    threaded_start = time.time()
    
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
        
        future_to_p = {executor.submit(fdp, net, p): p for p in param_list}
        for future in concurrent.futures.as_completed(future_to_p):
            p= future_to_p[future]
            try:
                data = future.result()
            except Exception as exc:
                print('%r generated an exception: %s' % (p, exc))
            else:
                print(f'P:{p}\n{data.tolist()}')
    print("Threaded time:", time.time() - threaded_start)

Result first execution:

Running threaded:
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P:50
[37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]
P:40
[37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]
P:20
[37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]
P:10
[37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]
P:30
[37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]
Threaded time: 10.289913654327393

Result after first execution:

gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
Running threaded:
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P:20
[36.928335306007504, 38.56727922941338, 40.443172514058745, 40.72394443104987, 40.90622400559749, 41.46540572324214, 40.675081019595225, 40.41174715256963, 40.00281050735774, 40.07920802307382, 40.38560471085664, 40.38193763174398, 40.37963668287398, 40.552739535506646]
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P:30
[36.928335306007504, 38.56727922941338, 40.443172514058745, 40.72394443104987, 40.90622400559749, 41.46540572324214, 40.675081019595225, 40.41174715256963, 40.00281050735774, 40.07920802307382, 40.38560471085664, 40.38193763174398, 40.37963668287398, 40.552739535506646]
P:10
[36.928335306007504, 38.56727922941338, 40.443172514058745, 40.72394443104987, 40.90622400559749, 41.46540572324214, 40.675081019595225, 40.41174715256963, 40.00281050735774, 40.07920802307382, 40.38560471085664, 40.38193763174398, 40.37963668287398, 40.552739535506646]
P:40
[36.99051367937562, 38.62972152989315, 40.52523119429694, 40.80411871269535, 40.988284684904144, 41.54941541992989, 40.70451861749286, 40.47434804855512, 40.10859494962387, 40.158882354320575, 40.46021745533423, 40.45586106415878, 40.46147336798991, 40.63538923218931]
P:50
[37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]
Threaded time: 5.970590829849243

Expected output:

gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P: 10MW 
[36.751272106399846, 38.38762996566227, 40.19506832628781, 40.430129260147005, 40.6234137762414, 41.2329955341445, 40.58851960709539, 40.22088592382624, 39.70638081851565, 39.78493232181634, 40.19846080489043, 40.19658795616762, 40.19163521434265, 40.34719067188158]
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P: 20MW 
[36.85372545393822, 38.491904324526544, 40.34131262933125, 40.61232778016679, 40.79622873797999, 41.366262933523764, 40.63911029771148, 40.33365020295586, 39.877037015925644, 39.967766585867494, 40.302060961496764, 40.29918919390446, 40.29206912477938, 40.46060120186742]
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P: 30MW 
[36.928335306007504, 38.56727922941338, 40.443172514058745, 40.72394443104987, 40.90622400559749, 41.46540572324214, 40.675081019595225, 40.41174715256963, 40.00281050735774, 40.07920802307382, 40.38560471085664, 40.38193763174398, 40.37963668287398, 40.552739535506646]
gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens.
P: 40MW 
[36.99051367937562, 38.62972152989315, 40.52523119429694, 40.80411871269535, 40.988284684904144, 41.54941541992989, 40.70451861749286, 40.47434804855512, 40.10859494962387, 40.158882354320575, 40.46021745533423, 40.45586106415878, 40.46147336798991, 40.63538923218931]

gen vm_pu > bus max_vm_pu for gens [2 3]. Setting bus limit for these gens. P: 50MW [37.05258282566508, 38.692084146840514, 40.607457023524645, 40.884405519711464, 41.07048955890102, 41.63367715315253, 40.7339315535116, 40.53692123467765, 40.214534189208756, 40.23866161530872, 40.534944260092104, 40.52988248975587, 40.543513373832106, 40.71825168461873]



Read more here: https://stackoverflow.com/questions/67014852/threadpoolexecutor-is-mixing-the-input-data-to-the-function-on-each-thread-wind

Content Attribution

This content was originally published by Christian Rueda at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: