def show(self): now = time.time() self.lock.acquire() # compute column widths maxi = max( [len(str(x)) for l in [self.jobs, self.waitjobs] for x in l] + [2]) maxn = max( [len(l[x].name) for l in [self.jobs, self.waitjobs] for x in l] + [4]) times = [ log.show_time(now - l[x].start) for l in [self.jobs, self.waitjobs] for x in l ] waittimes = [log.show_time(0.0) for x in self.waitjobs] maxt = max([len(t) for t in times] + [4]) maxto = max([ len(str(l[x].timeout)) for l in [self.jobs, self.waitjobs] for x in l if l[x].timeout is not None ] + [7]) maxs = max([len(x) for x in ['STATUS', 'running', 'waiting']]) # print header log.writeline('%*s %-*s %-*s %-*s %-*s' % (maxi, 'ID', maxn, 'NAME', maxt, 'TIME', maxto, 'TIMEOUT', maxs, 'STATUS')) log.writeline( '-' * maxi + ' ' + '-' * maxn + ' ' + '-' * maxt + ' ' + '-' * maxto + ' ' + '-' * maxs, log.Color.PURPLE) # sort by ID # print running jobs keys = sorted(list(self.jobs)) for i in range(0, len(self.jobs)): x = keys[i] log.writeline( '%*s %-*s %*s %-*s %-*s' % (maxi, x, maxn, self.jobs[x].name, maxt, times[i], maxto, '' if self.jobs[x].timeout is None else self.jobs[x].timeout, maxs, 'running')) # print waiting jobs keys = sorted(list(self.waitjobs)) for i in range(0, len(self.waitjobs)): x = keys[i] log.writeline( '%*s %-*s %*s %-*s %-*s' % (maxi, x, maxn, self.waitjobs[x].name, maxt, waittimes[i], maxto, '' if self.waitjobs[x].timeout is None else self.waitjobs[x].timeout, maxs, 'waiting')) self.lock.release()
def run(self): while not self.terminate: # quit program not requested self.lock.acquire() todel = [] # BACKGROUND JOBS for x in self.jobs: # pinpoint dead background jobs s = self.jobs[x] if not s.job.is_alive(): todel.append(x) for x in todel: # remove them s = self.jobs[x] end = time.time() log.info('Background job %d (%s) has terminated (%s).' % (x, s.name, log.show_time(end - s.start))) del self.jobs[x] # USER THREADS todel = [] for t in self.user_threads: # pinpoint dead user threads if not t.is_alive(): todel.append(t) for t in todel: # remove them self.user_threads.remove(t) # check waiting jobs # TODO todel = [] for k, v in self.waitjobs.items(): if len(set(v.waitfor).intersection(self.jobs.keys())) == 0: # move waitjob to jobs, start it todel.append(k) log.info('Background job %d (%s) is no longer waiting.' % (k, v.name)) start = time.time() v.job.start() v.start = start self.jobs[k] = v for x in todel: del self.waitjobs[x] self.lock.release() time.sleep(0.25)