I have data in below format: and I have function which accepts 2 keyword argument I am unable to come with syntax or example where i can pass specific keys from list of dictionaries to Map function to run as multi threading
import concurrent.futures
data = [
{
"name": "abc",
"org": "pqr"
},
{
"name": "xyz",
"org": "sdf"
}
]
def process_data(org_name, cu_name):
print(org_name)
print(cu_name)
with concurrent.futures.ThreadPoolExecutor() as Executor:
results = Executor.map(process_data, data)
Since data consists different key i need to map org to org_name , But I am not sure how to pass with map function
CodePudding user response:
When you use data as the iterable in map() what will happen is that each element in the list will be passed to process_data. That function only receives one parameter.
So...
from concurrent.futures import ThreadPoolExecutor as TPE
data = [
{
"name": "abc",
"org": "pqr"
},
{
"name": "xyz",
"org": "sdf"
}
]
def process_data(d):
for k, v in d.items():
print(f'{k} = {v}')
with TPE() as tpe:
tpe.map(process_data, data)
...may help to clarify
CodePudding user response:
Let your process_data
function accept the needed keys org
and name
, so each input dictionary can be then unpacked (**
) onto respective keys:
def process_data(org, name):
print(org, name)
with concurrent.futures.ThreadPoolExecutor() as Executor:
results = Executor.map(lambda kargs: process_data(**kargs), data)
pqr abc
sdf xyz
CodePudding user response:
import concurrent.futures
data = [
{
"name": "abc",
"org": "pqr"
},
{
"name": "xyz",
"org": "sdf"
}
]
def process_data(org_name, cu_name):
print(f'org = {org_name}')
print(f'cu ={cu_name}')
with concurrent.futures.ThreadPoolExecutor() as Executor:
result = Executor.map(lambda d: process_data(org_name=d['name'], cu_name=d['org']), data)
This worked for me thanks to Michael