from scipy.io import savemat
import time
import numpy as np
import multiprocessing as mp
import os
import argparse
def main(id):
if not os.path.isdir('./10/sub_{}/'.format(id)):
os.mkdir('./10/sub_{}/'.format(id))
start_time = time.time()
print('------ Generate data of id {} ----------'.format(id))
#task
print('Time for', d, time.time() - start_time)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Simulation')
parser.add_argument('--a_start', type=int, default=0, metavar='t/f', help='start id')
parser.add_argument('--a_end', type=int, default=20, metavar='t/f', help='end id')
args = parser.parse_args()
os.environ["MKL_NUM_THREADS"] = "1"
start_time = time.time()
# RUN THE CODE IN PARALLEL
processes = [mp.Process(target=main, args=(x,)) for x in range(args.a_start, args.a_end)]
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# NO PARALLEL
#for x in range(args.a_start, args.a_end):
# main(x)
print('Total_time', time.time() - start_time)
多进程简单示例
于 2024-05-17 12:49:13 首次发布