Python 为什么启用多处理的脚本只创建10个文件夹中的4个?
我试图通过使用多处理来修改脚本以跨多个应用程序复制文件,这是一个练习,让我进一步了解python中的多处理 我的主要工作就是这样Python 为什么启用多处理的脚本只创建10个文件夹中的4个?,python,multiprocessing,Python,Multiprocessing,我试图通过使用多处理来修改脚本以跨多个应用程序复制文件,这是一个练习,让我进一步了解python中的多处理 我的主要工作就是这样 if __name__ == "__main__": #get command line arguments cmdlineArgs = getCmdLineArguments() #get all the files in folder listOfFiles = getFiles(cmdlineArgs.sour
if __name__ == "__main__":
#get command line arguments
cmdlineArgs = getCmdLineArguments()
#get all the files in folder
listOfFiles = getFiles(cmdlineArgs.sourceDirectory)
#create dataframe of files which needs to be copied
filesDF = createDF(listOfFiles, cmdlineArgs.destDirectory)
processes = []
lstOfDates = list(set(filesDF['date'].to_list()))
lstOfDates.sort()
# for dt in lstOfDates:
# copyFilesAcross([filesDF, [dt]])
splitListOfDatesForProc = [(lstOfDates[i:i+3]) for i in range(0, len(lstOfDates), 3)]
for dt in splitListOfDatesForProc:
p = Process(target=copyFilesAcross, args=([filesDF, dt],))
processes.append(p)
p.start()
for p in processes:
p.join()
CopyFilesCross执行以下操作:
def copyFilesAcross(lst):
#keep only the date provided as parameter
df = lst[0]
dt = lst[1]
for d in dt:
df = df[df.date == d]
print("Processing date " + d + ' for PID: ', os.getpid())
for index,row in df.iterrows():
try:
#print('Making directory ' + row['destination'])
os.makedirs(row['destination'], exist_ok=True)
shutil.copy(row['source'], row['destination'])
except OSError as e:
print('Failed to copy file ' + row['source'] + ' with error {0}'.format(e) )
except:
print("Unexpected error: ", sys.exc_info()[0])
输出:
getFiles: Executed ...
getFiles: Creating empty list ...
getFiles: Concatenating files ...
Creating dataframe of files to be copied ...
Creating empty dataframe ...
Populating dataframe ...
Sorting data frame by date ...
Processing date 20180204 for PID: 35033 <- processed
Processing date 20180304 for PID: 35034 <- processed
Processing date 20180811 for PID: 35038 <- processed
Processing date 20180815 for PID: 35041 <- processed
Processing date 20180311 for PID: 35034 <- not processed
Processing date 20180724 for PID: 35034 <- not processed
Processing date 20180222 for PID: 35033 <- not processed
Processing date 20180303 for PID: 35033 <- not processed
Processing date 20180812 for PID: 35038 <- not processed
Processing date 20180813 for PID: 35038 <- not processed
Process finished with exit code 0
getFiles:已执行。。。
getFiles:正在创建空列表。。。
getFiles:正在连接文件。。。
正在创建要复制的文件的数据帧。。。
正在创建空数据帧。。。
正在填充数据帧。。。
按日期排序数据帧。。。
PID:35033的处理日期20180204这并不是一个多处理问题,只是代码中有一个bug
在copyFilesCross
中的第一次循环迭代中,覆盖df
,并丢弃除与dt
中的第一个日期匹配的行之外的每一行。在dt:
中d的下一次(以及所有后续)迭代中,您试图找到一个不存在的不同日期,然后用一个空数据框覆盖df
。当您为df.iterrows()中的索引行调用:
时,没有行,因此循环根本不会执行