def run_multiple_commands_redirect_stdout(<EOL>multiple_args_dict,<EOL>print_commands=True,<EOL>process_limit=-<NUM_LIT:1>,<EOL>polling_freq=<NUM_LIT:0.5>,<EOL>**kwargs):
assert len(multiple_args_dict) > <NUM_LIT:0><EOL>assert all(len(args) > <NUM_LIT:0> for args in multiple_args_dict.values())<EOL>assert all(hasattr(f, '<STR_LIT:name>') for f in multiple_args_dict.keys())<EOL>if process_limit < <NUM_LIT:0>:<EOL><INDENT>logger.debug("<STR_LIT>" % cpu_count())<EOL>process_limit = cpu_count()<EOL><DEDENT>start_time = time.time()<EOL>processes = Queue(maxsize=process_limit)<EOL>def add_to_queue(process):<EOL><INDENT>process.start()<EOL>if print_commands:<EOL><INDENT>handler = logging.FileHandler(process.redirect_stdout_file.name)<EOL>handler.setLevel(logging.DEBUG)<EOL>logger.addHandler(handler)<EOL>logger.debug("<STR_LIT:U+0020>".join(process.args))<EOL>logger.removeHandler(handler)<EOL><DEDENT>processes.put(process)<EOL><DEDENT>for f, args in multiple_args_dict.items():<EOL><INDENT>p = AsyncProcess(<EOL>args,<EOL>redirect_stdout_file=f,<EOL>**kwargs)<EOL>if not processes.full():<EOL><INDENT>add_to_queue(p)<EOL><DEDENT>else:<EOL><INDENT>while processes.full():<EOL><INDENT>to_remove = []<EOL>for possibly_done in processes.queue:<EOL><INDENT>if possibly_done.poll() is not None:<EOL><INDENT>possibly_done.wait()<EOL>to_remove.append(possibly_done)<EOL><DEDENT><DEDENT>if to_remove:<EOL><INDENT>for process_to_remove in to_remove:<EOL><INDENT>processes.queue.remove(process_to_remove)<EOL><DEDENT>break<EOL><DEDENT>time.sleep(polling_freq)<EOL><DEDENT>add_to_queue(p)<EOL><DEDENT><DEDENT>while not processes.empty():<EOL><INDENT>processes.get().wait()<EOL><DEDENT>elapsed_time = time.time() - start_time<EOL>logger.info(<EOL>"<STR_LIT>",<EOL>len(multiple_args_dict),<EOL>elapsed_time)<EOL>
Run multiple shell commands in parallel, write each of their
stdout output to files associated with each command.

Parameters
----------
multiple_args_dict : dict
    A dictionary whose keys are files and values are args list.
    Run each args list as a subprocess and write stdout to the
    corresponding file.

print_commands : bool
    Print shell commands before running them.

process_limit : int
    Limit the number of concurrent processes to this number. 0
    if there is no limit, -1 to use max number of processors

polling_freq : int
    Number of seconds between checking for done processes, if
    we have a process limit