使用多处理编写JSON文件 - Python

时间:2018-06-12 03:11:58

标签: python json multiprocessing

我有一个JSON,它有一些必须提取,处理和使用的值,为文件添加新值。为此,我使用多处理,虽然它是先验同步,但我得到了竞争条件。

被调用的函数只是将0-5范围内的等级转换为0-100范围。

提前致谢!

import json
import multiprocessing

n = 1000

maxRating = 5
percentage = 100

inputfile= 'rawData.JSON'

outfile= 'processedData.JSON'

#load data into dictionary "data"
with open(inputfile) as f:
    data = json.load(f)

#create an empty dictionary that will contain the new informations 
results = {}

def saver(init,end,q,l):
    for num in range(init, end):
        l.acquire()
        rating = data["bars"][num]["rating"]

        ratioRating = (percentage * rating) / maxRating

        results["ratingP"] = ratioRating
        print(ratioRating)
        #put data in queue
        q.put(results)
        l.release()

#main function
if __name__ == '__main__':
    i = 0
    cores = 4

    q = multiprocessing.Queue() 

    lock = multiprocessing.Lock()

    if(cores > 1): #parallel
        for i in range (cores):
            init = (i*n)/cores
            fin = ((i+1)*n)/cores
            p = multiprocessing.Process(target = saver, args = (init,fin,q,lock)).start()

        for i in range (n):
            data["bars"][i].update(q.get()) #update "data" dictionary adding new processed data

    else: #sequential
        saver(0,n,q)
        for l in range (n):
            data["bars"][l].update(q.get()) #update "data" dictionary adding new processed data

    #write the updated JSON file with the added processed data
    with open(outfile,'w') as outfile:
        json.dump(data,outfile)

0 个答案:

没有答案