|
a |
|
b/modas/multiprocess.py |
|
|
1 |
import multiprocessing as mp |
|
|
2 |
import subprocess |
|
|
3 |
import os |
|
|
4 |
|
|
|
5 |
def run(cmd): |
|
|
6 |
""" |
|
|
7 |
run linux shell by subprocess |
|
|
8 |
""" |
|
|
9 |
null_f = open(os.devnull, 'w') |
|
|
10 |
a = subprocess.call(cmd, shell=True, stdout=null_f, stderr=null_f) |
|
|
11 |
null_f.close() |
|
|
12 |
|
|
|
13 |
return a |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def parallel(run, values, num_threads=1): |
|
|
17 |
"""parallel run linux command line""" |
|
|
18 |
try: |
|
|
19 |
p = mp.Pool(processes=num_threads) |
|
|
20 |
results = [p.apply_async(run, cmd) for cmd in values] |
|
|
21 |
p.close() |
|
|
22 |
p.join() |
|
|
23 |
|
|
|
24 |
except KeyboardInterrupt as e: |
|
|
25 |
p.terminate() |
|
|
26 |
p.join() |
|
|
27 |
raise e |
|
|
28 |
return [result.get() for result in results] |