1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-21 19:16:30 +00:00

Merge pull request #9 from Ahp06/multiprocessing

Multiprocessing
This commit is contained in:
Axel HUYNH-PHUC 2019-01-24 11:37:29 +01:00 committed by GitHub
commit de95e718c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 22 deletions

View File

@ -42,7 +42,7 @@ class Config:
:param config_file: The path to your configuration file :param config_file: The path to your configuration file
:return: :return:
""" """
with open(f'files/configs/{config_file}.json', 'r') as f: with open(f'{config_file}.json', 'r') as f:
data = json.load(f) data = json.load(f)
for option in data: for option in data:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -182,7 +182,7 @@ def create_dump(dump_name, simulation_dir, areas_number):
print(f'Dump with name {dump_name} already exist') print(f'Dump with name {dump_name} already exist')
traci.close(False) traci.close(False)
def add_options(parser): def add_options(parser):
""" """
Add command line options Add command line options
@ -194,21 +194,39 @@ def add_options(parser):
# Faire que -c soit requis si -run # Faire que -c soit requis si -run
parser.add_argument("-new_dump", "--new_dump", type=str, parser.add_argument("-new_dump", "--new_dump", type=str,
required=False, help='Load and create a new data dump with the configuration file chosen') help='Load and create a new data dump with the configuration file chosen')
parser.add_argument("-areas", "--areas", type=int, required=False, parser.add_argument("-areas", "--areas", type=int,
help='Will create a grid with "areas x areas" areas') help='Will create a grid with "areas x areas" areas')
parser.add_argument("-simulation_dir", "--simulation_dir", type=str, required=False, parser.add_argument("-simulation_dir", "--simulation_dir", type=str,
help='Choose the simulation directory') help='Choose the simulation directory')
parser.add_argument("-run", "--run", type=str, parser.add_argument("-run", "--run", type=str,
help='Run a simulation process with the dump chosen') help='Run a simulation process with the dump chosen')
parser.add_argument("-c", "--c", metavar =('config1','config2'), nargs='+', type=str, parser.add_argument("-c", "--c", metavar =('config1','config2'), nargs='+', type=str,
help='Choose your(s) configuration file(s) from your working directory') help='Choose your(s) configuration file(s) from your working directory')
parser.add_argument("-c_dir", "--c_dir", type=str,
help='Choose a directory which contains your(s) configuration file(s)')
parser.add_argument("-save", "--save", action="store_true", parser.add_argument("-save", "--save", action="store_true",
help='Save the logs into the logs folder') help='Save the logs into the logs folder')
parser.add_argument("-csv", "--csv", action="store_true", parser.add_argument("-csv", "--csv", action="store_true",
help="Export all data emissions into a CSV file") help="Export all data emissions into a CSV file")
def check_user_entry(args):
"""
Check the user entry consistency
"""
if (args.new_dump is not None):
if(args.areas is None or args.simulation_dir is None):
print('The -new_dump argument requires the -areas and -simulation_dir options')
return False
if (args.run is not None):
if(args.c is None and args.c_dir is None):
print('The -run argument requires the -c or -c_dir')
return False
return True
def main(args): def main(args):
""" """
The entry point of the application The entry point of the application
@ -219,28 +237,40 @@ def main(args):
add_options(parser) add_options(parser)
args = parser.parse_args(args) args = parser.parse_args(args)
if args.new_dump is not None: if(check_user_entry(args)):
if (args.simulation_dir is not None) and (args.areas is not None):
create_dump(args.new_dump, args.simulation_dir, args.areas) if args.new_dump is not None:
if (args.simulation_dir is not None) and (args.areas is not None):
if args.run is not None: create_dump(args.new_dump, args.simulation_dir, args.areas)
dump_path = f'files/dump/{args.run}.json'
if os.path.isfile(dump_path): if args.run is not None:
with open(dump_path, 'r') as f: dump_path = f'files/dump/{args.run}.json'
data = jsonpickle.decode(f.read()) if os.path.isfile(dump_path):
with open(dump_path, 'r') as f:
process = [] data = jsonpickle.decode(f.read())
if args.c is not None: process = []
for conf in args.c: # Initialize all process files = []
if args.c is not None:
for config in args.c:
files.append(f'files/configs/{config}')
if args.c_dir is not None:
path = f'files/configs/{args.c_dir}/'
bundle_files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
for config in bundle_files:
conf_formatted = config.replace('.json','')
files.append(f'files/configs/{args.c_dir}/{conf_formatted}')
for conf in files: # Initialize all process
config = Config(conf,data) config = Config(conf,data)
p = RunProcess(data, config,args.save,args.csv) p = RunProcess(data, config,args.save,args.csv)
process.append(p) process.append(p)
p.start() p.start()
for p in process : p.join() for p in process : p.join()
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) main(sys.argv[1:])