Python多处理池循环读写文本fi

2024-06-26 00:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用多处理模块并行化一个用于读写大量文本文件的循环。 这是我的问题: 我有一个名为SWAT的类,其中包括几个方法。一个名为“auto”的方法尝试自动化所有其他方法。它需要3个参数。 我能做些什么来并行循环?在

def auto (self, directory, number_samples, number_trajectories, grid_jump):

import glob, os
    os.chdir (directory)
    a = SWAT.generate_samples (self, number_samples, number_trajectories, 
grid_jump)
    b = [i.split (',') for i in SWAT.readlist (self, 'rng.txt')]

    for i in range (len(a)): 
        path = directory + '\\TxtInOut{}'. format (i)
        os.mkdir (path)
        sample = open ('sample.txt', 'w')
        for j in range (len (b)):
            sample.write ('{},{},{},{}\n'. format (b [j] [0], b [j] [1], b [j] [2], a [i] [j]))
        sample.close ()

        for file in glob.glob ('*cio'):
        e = SWAT.file_cio (self, file)
        c = SWAT.fig_fig (self, e [1] [6] [1])
        SWAT.wwq_write (self, SWAT.basins_bsn (self, e [1] [46] [1]) [1] [63] [1], path) 
        SWAT.basins_bsn_write (self, e [1] [46] [1], path)
        for j in c [0]:
            d = SWAT.subbasin (self, c [0] [j])
            SWAT.pnd_write (self, d [1] [30] [1], path)
            for k in d [3]:
                SWAT.hru_write (self, d [3] [k] [0], path)
                SWAT.mgt_write (self, d [3] [k] [1], path)
                SWAT.gw_write (self, d [3] [k] [4], path)
                SWAT.sep_write (self, d [3] [k] [6], path)
            SWAT.subbasin_write (self, c [0] [j], path)
        for j in c [1]:
            SWAT.rte_write (self, c [1] [j] [0], path)
            SWAT.swq_write (self, c [1] [j] [1], path)
            SWAT.file_cio_write (self, file, path)

Tags: samplepath方法inselfnumberforos