如何修复“ Python中的pool.map问题”

时间:2019-10-29 15:50:17

标签: python parallel-processing

我正在尝试在python中进行多处理。任务是使用pool.map在文件夹中生成文本文件。谁能简短解释一下代码出了什么问题,为什么它会进入无限循环而又没有任何活动发生?另外,我无法使用ctrl + c退出正在运行的程序。我提供了MWE。我们非常感谢您的帮助。

# =============================================================================
# Minimum Working Example
# =============================================================================
import os
from multiprocessing import Pool

path=os.getcwd() # address of current working directory
# =============================================================================
basemodel='Example_SS1' # Enter the basemodel name

try:
    os.mkdir(os.path.join(path, basemodel)) # create a directory if do not exists
except Exception:
    pass # if existing, pass on (i.e. continue)
# =============================================================================
Hmax=[0.93,0.93,1.86,1.86,2.79,2.79,3.72,3.72,4.65,5.58] 
Tp=[3.88,6.46,5.17,7.75,6.46,9.04,7.75,10.34,9.04,10.34] 
drns=[0,30,60,90,120,150,180]
# =============================================================================
all_files=[]
count=1
for k in range(0,len(drns)):
    for i in range(0,len(Hmax)):
        file_name=str(str(count).zfill(2))+'_Hmax_'+str(Hmax[i])+'_Tp_'+str(Tp[i])+'_'+str(drns[k]) # filename syntax of loadcases
        print (file_name)
        all_files.append(file_name)
        count+=1 # loadcase number increment
# =============================================================================                
def todos(filename):
    lc_split=filename.split("_")
    lc_number=int(lc_split[0])
    lc_Hmax=float(lc_split[2])
    lc_Tp=float(lc_split[4])
    lc_drn=float(lc_split[5])      
    # =============================================================================
    file_name=str(str(lc_number).zfill(2))+'_Hmax_'+str(lc_Hmax)+'_Tp_'+str(lc_Tp)+'_'+str(lc_drn)+'.txt' # filename syntax of loadcases
    os.path.join(path, basemodel, file_name)
    print ('Loadcase '+file_name+' is created')
    # =============================================================================

if __name__ == "__main__":
    pool = Pool(24)
    pool.map(todos, all_files)
pool.close()

0 个答案:

没有答案