#!/usr/bin/python #!/home/tuma/vtkbin/vtkpython import sys import commands import os import shutil import time from threading import Thread nproc = int(sys.argv[2]) class task(Thread): def __init__ (self,cmd): Thread.__init__(self) self.cmdline = cmd self.status = -1 def run(self): print 'running cmd "%s"\n (%s)'%(self.cmdline,time.strftime("%d.%m.%Y %H:%M:%S", time.localtime())) output=commands.getoutput(self.cmdline) #output = os.popen(self.cmdline) print "command %s finished. Output:"%(self.cmdline) print "#####################################" print output print "#####################################" self.status = 0 queue=sys.argv[1] completed=queue + ".completed" try: fcomp=open(completed,"a") except IOError: exit(0) #shutil.copyfile(queue, "%s.orig"%queue) def firstinqueue(): try: f=open(queue,"r") except IOError: return 0 lines = f.readlines() f.close() cmd=lines[0][0:-1] if(len(lines[1:])>0): f=open(queue,"w") f.writelines(lines[1:]) f.close() else: os.unlink(queue) return cmd def waitloop(cmdlist): while len(cmdlist) >= nproc: for t in cmdlist: if t.status == 0: cmdlist.remove(t) time.sleep(1) cmdlist = [] cmd = firstinqueue() while(cmd): if len(cmdlist) >= nproc: waitloop(cmdlist) current = task(cmd) cmdlist.append(current) current.start() fcomp.write(cmd+"\n") fcomp.flush() cmd = firstinqueue() fcomp.close()