Python Multiprocessing on List of dictionaries

*爱你&永不变心* 提交于 2020-05-15 02:57:07

问题


I have a list of dictionaries. list_of_dict = [{'name' : 'bob', 'weight': 50}, {'name' : 'ramesh', 'weight': 60}]

I want to process both the dictionaries at the same time

What should I use for this multiprocessing Pool or Process?


回答1:


I have tried with Multiprocessing Pool

from multiprocessing.pool import ThreadPool as Pool

pool_size = 5 

def worker(item1, itme2):
    try:
        print(item1.get('weight'))
        print(itme2)
    except:
        print('error with item')

pool = Pool(pool_size)
items = [{'name' : 'bob', 'weight': 50}, {'name' : 'ramesh','weight': 60}]
for item in items:
    pool.apply_async(worker, (item, 'item2'))

pool.close()
pool.join()



回答2:


I use the following because it is simple and easy to understand:

from multiprocessing.dummy import Pool as ThreadPool

def threadfunction(dict):
    weight = dict.get('weight')
    return weight

pool = ThreadPool(5)
threadresults = pool.map(threadfunction,list_of_dict)
pool.close()
pool.join()

It will handle the mapping of the the list: list_of_dict. So you just need to pass in the number of threads to the ThreadPool() function and pass the function and list to the pool.map function. Simple!



来源:https://stackoverflow.com/questions/59086617/python-multiprocessing-on-list-of-dictionaries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!