Python代码for循环部分,可以做出想java一样多进程的呢,因为数据量比较大,跑的时间有点长了

fp1 = open("C:\\a.txt",'r')

fp2 = open("C:\\b.csv",'w')

fp2.write('ab''\n')

for line in fp1:

line = line.rstrip().lstrip('"').rstrip('"')

print("checking MD5 " + line)

return_code = getResult(line)

if return_code == 1:

fp2.write(line + '\n')

else:

continue

fp1.close()

fp2.close()
如何像java一样做出多进程的呢

from multiprocessing import Pool

fp2 = open("C:\\b.csv",'w')
fp2.write('ab''\n')

def process_line(line):
    line = line.rstrip().lstrip('"').rstrip('"')    
    print("checking MD5 " + line)  
    return_code = getResult(line)  
    if return_code == 1:      
        fp2.write(line + '\n')  

if __name__ == "__main__":
    pool = Pool(4)
    with open('C:\\a.txt') as source_file:
        # chunk the work into batches of 4 lines at a time
        pool.map(process_line, source_file, 4)

试试这个。

追问

这个是开了几个进程在跑呢,4个吗???

追答

恩,就是那两个4,Pool(4)是4个进程,pool.map(process_line, source_file, 4),是文件切了4块

追问

大哥,发现了一个错误,为什么不同的进程跑出来的数据不同呢,是不是哪里错误了,你再认真看看,我分别用4个和10个进程跑数据,得出的数据不同呀,进程越多,跑出来的数据就越少,这是神马情况,请大神再认真看看,
我又重新用原来的程序跑了一遍,还是对不上你的这个???why???

追答

这个程序我没测试,因为没有数据,要不你给我发一个少点的数据,还有getResult这个方法发给我

追问

无法发私信给你呀

追答

84296733 QQ邮箱

温馨提示:答案为网友推荐,仅供参考
相似回答