通过多处理附加值时获取一个空列表



我正在使用python 3.5编辑列表,在这种情况下为 predictions_dict['D'],包含在字典predictions_dict中。这是我使用的代码:

import multiprocessing as multip
predictions_dict = {'A': [],
                    'B': [],
                    'C': [],
                    'D': [],
                    'E': [],
                    'F': [],
                    'Def': []}
data = [{'index': 1, 'rank': 'A'}, {'index': 2, 'rank': 'D'}, {'index': 3, 'rank': 'E'}]
prediction = [(1, 'C'), (2, 'D'), (3, 'D')]

def create_predictions_dict(index, rank):
    for j in data:
        if j['index'] == index:
            predictions_dict[rank].append((index, j['rank'], rank))
            break
np = multip.cpu_count()
p = multip.Pool(processes=np)
_ = p.starmap(create_predictions_dict, prediction)
p.close()
p.join()
print('final list:', predictions_dict['D'])

当我执行此代码时,我得到的输出是:

final list: []

我不明白为什么,正如我期望得到的那样:

final list: [(2, 'D', 'D'), (3, 'E', 'D')]

我已经从事解决方案的工作,这要归功于在评论中确定该问题不共享状态的事实:

import multiprocessing as multip
predictions_dict = {'A': [],
                    'B': [],
                    'C': [],
                    'D': [],
                    'E': [],
                    'F': [],
                    'Def': []}
data = [{'index': 1, 'rank': 'A'}, {'index': 2, 'rank': 'D'}, {'index': 3, 'rank': 'E'}]
prediction = [(1, 'C'), (2, 'D'), (3, 'D')]

def create_predictions_dict(index, rank):
    for j in data:
        if j['index'] == index:
            return index, j['rank'], rank
np = multip.cpu_count()
p = multip.Pool(processes=np)
sk = p.starmap(create_predictions_dict, prediction)
p.close()
p.join()
for elem in sk:
    predictions_dict[elem[2]].append(elem)
print('final list:', predictions_dict['D'])

相关内容

  • 没有找到相关文章

最新更新