-
Notifications
You must be signed in to change notification settings - Fork 0
/
multijob.py
31 lines (30 loc) · 984 Bytes
/
multijob.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
import subprocess
import time
class multijob(object):
def __init__(self, max_jobs, poll_interval):
self.max_jobs = max_jobs
self.poll_interval = poll_interval
self.jobs = []
def run_job(self, args, nice=None):
while not (len(self.jobs) < self.max_jobs or self.poll_jobs()):
time.sleep(self.poll_interval)
if nice is not None:
def set_nice():
import os
os.nice(nice)
self.jobs.append(subprocess.Popen(args,preexec_fn=set_nice))
else:
self.jobs.append(subprocess.Popen(args))
def wait_all(self):
for child in self.jobs:
child.wait()
self.jobs.remove(child)
def poll_jobs(self):
for child in self.jobs:
res = child.poll()
if res is not None:
self.jobs.remove(child)
return True
return False
def __del__(self):
self.wait_all()