-
Notifications
You must be signed in to change notification settings - Fork 10
/
tools.py
53 lines (44 loc) · 1.6 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Author: Maxwell I. Zimmerman <[email protected]>
# Contributors:
# Copywright (C) 2017, Washington University in St. Louis
# All rights reserved.
# Unauthorized copying of this file, via any medium, is strictly prohibited
# Proprietary and confidential
#######################################################################
# imports
#######################################################################
import itertools
import numpy as np
import subprocess as sp
import sys
from multiprocessing import Pool
#######################################################################
# code
#######################################################################
def convert_to_string(binary):
return binary.decode('utf-8')
def _run_command(cmd_info):
"""Helper function for submitting commands parallelized."""
cmd, supress = cmd_info
p = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
output, err = p.communicate()
if convert_to_string(err) != '' and not supress:
print("\nERROR: " + convert_to_string(err))
raise
output = convert_to_string(output)
p.terminate()
return output
def run_commands(cmds, supress=False, n_procs=1):
"""Wrapper for submitting commands to shell"""
if type(cmds) is str:
cmds = [cmds]
if n_procs == 1:
outputs = []
for cmd in cmds:
outputs.append(_run_command((cmd, supress)))
else:
cmd_info = list(zip(cmds, itertools.repeat(supress)))
pool = Pool(processes = n_procs)
outputs = pool.map(_run_command, cmd_info)
pool.terminate()
return outputs