在python中进行多处理时更改全局变量

use*_*020 5 python multiprocessing python-2.7

所以我最终要做的是读取一行,用该行中的信息进行一些计算,然后将结果添加到某个全局对象,但我似乎永远无法让它工作.例如,下面的代码中的测试始终为0.我知道这是错的,我尝试过其他方式,但它仍然没有用.

import multiprocessing as mp

File = 'HGDP_FinalReport_Forward.txt'
#short_file = open(File)
test = 0

def pro(temp_line):
    global test
    temp_line = temp_line.strip().split()
    test = test + 1
    return len(temp_line)

if __name__ == "__main__":
    with open("HGDP_FinalReport_Forward.txt") as lines:
        pool = mp.Pool(processes = 10)
        t = pool.map(pro,lines.readlines())
Run Code Online (Sandbox Code Playgroud)

Fre*_*Foo 16

池生成的工作进程获取自己的全局变量副本并更新它.除非您明确设置,否则它们不共享内存.最简单的解决方案是将最终值test传回主进程,例如通过返回值.像(未经测试)的东西:

def pro(temp_line):
    test = 0
    temp_line = temp_line.strip().split()
    test = test + 1
    return test, len(temp_line)

if __name__ == "__main__":
    with open("somefile.txt") as lines:
        pool = mp.Pool(processes = 10)
        tests_and_t = pool.map(pro,lines.readlines())
        tests, t = zip(*test_and_t)
        test = sum(tests)
Run Code Online (Sandbox Code Playgroud)

  • 这里的关键是,使用`multiprocessing`,线程(井,进程)不共享状态. (8认同)
  • +1代表答案,+1代表@Lattyware.我希望多处理文档更清楚一点"使用类似于线程模块的API的产生过程"与"创建线程"的区别是什么,因为这样可以解决SO上模块的一半问题...... (2认同)