pythonマルチプロセスデータ共有ManagerのBUG

1028 ワード

# -*- coding: utf-8 -*-
from multiprocessing import Pool, Manager
#from multiprocessing.dummy import Pool
dst=['123','123','234']


shared_dict_count = Manager().dict()
shared_dict_wrong = Manager().dict()
shared_dict_count['math']=1
# shared_dict_count = {}
# shared_dict_wrong = {}



def post(dst):
    for i in range(10):
        print(i)
        shared_dict_count['math']+=1
    print('11111',shared_dict_count['math'])



if __name__ == "__main__":
    pool = Pool(30)
    r=pool.map(post, dst)   #  ,   
    #pool.map(post, ['969.jpg'])
    pool.close()
    pool.join()

    print('last',shared_dict_count['math'])

 
 
プロセスプールで3つのプロセスを同時に走ると、渋滞が発生し、失われることがわかりました.毎回結果が異なる.
 
ocr@MEGVII1010525222:~/chenyu/ocr/EAST_keras$ python try2.py  0 0 0 1 1 2 2 1 3 3 4 4 2 5 5 3 6 6 4 7 7 5 8 6 8 9 7 9 ('11111', 12) 8 ('11111', 13) 9 ('11111', 15) ('last', 15)