我最终想做的事情是读取一行文本,对其中的信息进行一些计算,然后将结果添加到全局对象中,但似乎无法实现。例如,在下面的代码中,test始终为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())