如何 运行 一次批处理 5 个文件,然后继续处理列表中的下 5 个文件?
How to run batch process 5 files at a time, then continue on with the next 5 files that are within a list?
我有一些代码,其中有一个列表,其中包含一些 .bat 文件,稍后我会在其中一次执行所有这些 bat 文件。但是我想做的是一次只执行 5 个,然后一旦处理完成,它就会转到该列表中的下一个 5。
for ProcProfileCFG in ProcProfileCFGs
IETrajBatFile = ProcProfileCFG[3]
ProcProfileFile = ProcProfileCFG[2]
#This command then runs all of the bat files with in ProcProfileCFGs at once
#where x[3] is the bat file name i.e. "IE_trajectory.bat"
res = Pool().amap(os.system, ['powershell.exe ./' + x[3] for x in ProcProfileCFGs])
所以我只想运行前5个文件的这个命令,所以它们都同时运行然后一旦这些文件完成运行接下来的5个,依此类推,直到它通过所有这些。而不是一次全部,这是它目前正在做的。
使用multiprocessing
。例如,读取 CSVs
,命名为 file1.csv
,file2.csv
,等等:
import multiprocessing
output=[]
data = range(0,len(files))
def f(x):
pd.read_csv(file_name+x+'.csv')
def mp_handler():
p = multiprocessing.Pool(64)
r=p.map(f, data)
return r
if __name__ == '__main__':
output.append(mp_handler())
您可以在创建 Pool 时指定工作器的数量。
with Pool(5) as p:
p.map(func, list)
p.close()
p.join()
请注意,它不会同时处理相同的 5 个文件,而是按顺序移动到接下来的 5 个文件。但是它创建了一个有 5 个工人的队列,当一个工人完成时,另一个工人开始。它确保您一次只能处理 5 个文件。
将批处理文件分割成块
导入多处理
def handle_batch_file(file_name):
# dummy code
# print(file_name)
return file_name
BATCH_SIZE = 5
BATCH_FILES = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
def main():
p = multiprocessing.Pool(BATCH_SIZE)
counter = 0
while True:
sub_list = BATCH_FILES[counter * BATCH_SIZE: (counter + 1) * BATCH_SIZE]
print('Calling "map" with sub list {}'.format(sub_list))
dummy_results = p.map(handle_batch_file, sub_list)
if len(BATCH_FILES) <= (counter + 1) * BATCH_SIZE:
break
counter += 1
if __name__ == "__main__":
main()
输出
Calling "map" with sub list ['a', 'b', 'c', 'd', 'e']
Calling "map" with sub list ['f', 'g', 'h', 'i', 'j']
Calling "map" with sub list ['k', 'l', 'm', 'n']
我有一些代码,其中有一个列表,其中包含一些 .bat 文件,稍后我会在其中一次执行所有这些 bat 文件。但是我想做的是一次只执行 5 个,然后一旦处理完成,它就会转到该列表中的下一个 5。
for ProcProfileCFG in ProcProfileCFGs
IETrajBatFile = ProcProfileCFG[3]
ProcProfileFile = ProcProfileCFG[2]
#This command then runs all of the bat files with in ProcProfileCFGs at once
#where x[3] is the bat file name i.e. "IE_trajectory.bat"
res = Pool().amap(os.system, ['powershell.exe ./' + x[3] for x in ProcProfileCFGs])
所以我只想运行前5个文件的这个命令,所以它们都同时运行然后一旦这些文件完成运行接下来的5个,依此类推,直到它通过所有这些。而不是一次全部,这是它目前正在做的。
使用multiprocessing
。例如,读取 CSVs
,命名为 file1.csv
,file2.csv
,等等:
import multiprocessing
output=[]
data = range(0,len(files))
def f(x):
pd.read_csv(file_name+x+'.csv')
def mp_handler():
p = multiprocessing.Pool(64)
r=p.map(f, data)
return r
if __name__ == '__main__':
output.append(mp_handler())
您可以在创建 Pool 时指定工作器的数量。
with Pool(5) as p:
p.map(func, list)
p.close()
p.join()
请注意,它不会同时处理相同的 5 个文件,而是按顺序移动到接下来的 5 个文件。但是它创建了一个有 5 个工人的队列,当一个工人完成时,另一个工人开始。它确保您一次只能处理 5 个文件。
将批处理文件分割成块
导入多处理
def handle_batch_file(file_name):
# dummy code
# print(file_name)
return file_name
BATCH_SIZE = 5
BATCH_FILES = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
def main():
p = multiprocessing.Pool(BATCH_SIZE)
counter = 0
while True:
sub_list = BATCH_FILES[counter * BATCH_SIZE: (counter + 1) * BATCH_SIZE]
print('Calling "map" with sub list {}'.format(sub_list))
dummy_results = p.map(handle_batch_file, sub_list)
if len(BATCH_FILES) <= (counter + 1) * BATCH_SIZE:
break
counter += 1
if __name__ == "__main__":
main()
输出
Calling "map" with sub list ['a', 'b', 'c', 'd', 'e']
Calling "map" with sub list ['f', 'g', 'h', 'i', 'j']
Calling "map" with sub list ['k', 'l', 'm', 'n']