所以我最终要做的就是读一行,用该行中的信息做一些计算,然后将结果添加到某个全局对象,但我似乎永远无法让它工作。例如,下面的代码中的测试始终为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())
答案 0 :(得分:15)
池生成的工作进程获取自己的全局变量副本并更新它。除非您明确设置,否则它们不共享内存。最简单的解决方案是将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)
答案 1 :(得分:0)
以下是在多处理中使用全局变量的示例。
我们可以清楚地看到每个流程都使用自己的变量副本:
import multiprocessing
import time
import os
import sys
import random
def worker(a):
oldValue = get()
set(random.randint(0, 100))
sys.stderr.write(' '.join([str(os.getpid()), str(a), 'old:', str(oldValue), 'new:', str(get()), '\n']))
def get():
global globalVariable
return globalVariable
globalVariable = -1
def set(v):
global globalVariable
globalVariable = v
print get()
set(-2)
print get()
processPool = multiprocessing.Pool(5)
results = processPool.map(worker, range(15))
<强>输出:强>
27094 0 old: -2 new: 2
27094 1 old: 2 new: 95
27094 2 old: 95 new: 20
27094 3 old: 20 new: 54
27098 4 old: -2 new: 80
27098 6 old: 80 new: 62
27095 5 old: -2 new: 100
27094 7 old: 54 new: 23
27098 8 old: 62 new: 67
27098 10 old: 67 new: 22
27098 11 old: 22 new: 85
27095 9 old: 100 new: 32
27094 12 old: 23 new: 65
27098 13 old: 85 new: 60
27095 14 old: 32 new: 71