首页 > 解决方案 > 多处理工作者返回错误响应python

问题描述

请问你能帮帮我吗。

我写了一个与以下非常相似的脚本。函数返回的get_examples(word)示例word,我不知道多处理工作者出了什么问题。当words list很长时,get_examples(word)函数不会返回 的示例word,而是返回 的示例get_examples(other_word)

该错误仅在words list长时发生。

from multiprocessing.dummy import Pool as ThreadPool
def get_examples(word):
    #do someting ...
    eg = f'eg: {word}'
    return eg
def worker(word):
    data={}
    data['word'] = word
    data['examples'] = get_examples(word) #error??? 
    with open(f'{word}.json', 'w') as outfile:
        json.dump(data, outfile, indent=4)
       
words=['hi','go']
pool = ThreadPool(100)
pool.starmap(worker, zip(words)) 
pool.close() 
pool.join()

我得到这样的json文件:

hi.json --> {'word': 'hi','examples': 'eg: go'}
go.json --> {'word': 'go','examples': 'eg: hi'}

这是我的真实代码

标签: pythonmultithreadingmultiprocessing

解决方案


推荐阅读