使用多处理Python编写JSON文件

2024-04-25 20:15:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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)

Tags: theinjsonnewfordatadictionaryinit