def run(self): # print notification #logger = logging.getLogger(__name__ + '.Worker') while True: # Get next task from the queue (blocks thread if queue is empty until entry arrives) try: entry = self.tasks.get(True, self.queue_wait_time) # Wait five seconds for a new entry in the queue and check if we should shutdown if nothing shows up except: # Check if we should kill the thread if(self.shutdown_event.isSet()): # _sprint ("Queue Empty, exiting worker thread") self.deadthreadqueue.put(self) return else: #logger.info("Thread #%d idle shutdown" % (self.ident)) self.deadthreadqueue.put(self) return # Record start time so we get a sense of performance task_start_time = time.time() # _sprint("+++ {0}".format(entry.name)) #logger.info("+++ {0}".format(entry.name)) # do it! try: proc = subprocess.Popen(entry.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, *entry.args, **entry.kwargs) entry.returned_value = proc.communicate(input) entry.stdoutdata = entry.returned_value[0].decode('utf-8') entry.stderrdata = entry.returned_value[1].decode('utf-8') entry.returncode = proc.returncode proc = None except Exception as e: # inform operator of the name of the task throwing the exception # also, intercept the traceback and send to stderr.write() to avoid interweaving of traceback lines from parallel threads error_message = "\n*** {0}\n{1}\n{2}\n".format(entry.name, entry.args, traceback.format_exc()) #logger.error(error_message) sys.stderr.write(error_message) entry.exception = e entry.stdoutdata = None entry.returned_value = None entry.returncode = -1 entry.stderrdata = None # calculate finishing time and mark task as completed task_end_time = time.time() t_delta = task_end_time - task_start_time # mark the object event as completed entry.completed.set() # generate the time elapsed string for output seconds = math.fmod(t_delta, 60) seconds_str = "%02.5g" % seconds time_str = str(time.strftime('%H:%M:', time.gmtime(t_delta))) + seconds_str # print the completion notice with times aligned time_position = 70 out_string = "--- {0}".format(entry.name) out_string += " " * (time_position - len(out_string)) out_string += time_str #logging.info(out_string) # _sprint (out_string) JobsQueued = self.tasks.qsize() if JobsQueued > 0: JobQText = "Jobs Queued: " + str(self.tasks.qsize()) JobQText = ('\b' * 40) + JobQText + (' ' * (40 - len(JobQText))) pools._PrintProgressUpdate (JobQText) self.tasks.task_done()
def PrintJobsCount(): global ActiveJobCount JobQText = "Jobs Queued: " + str(ActiveJobCount) JobQText = ('\b' * 40) + JobQText + (' ' * (40 - len(JobQText))) nornir_pools._PrintProgressUpdate(JobQText)
def run(self): # print notification #logger = logging.getLogger(__name__ + '.Worker') while True: # Get next task from the queue (blocks thread if queue is empty until entry arrives) try: entry = self.tasks.get( True, self.queue_wait_time ) # Wait five seconds for a new entry in the queue and check if we should shutdown if nothing shows up except: # Check if we should kill the thread if (self.shutdown_event.isSet()): # _sprint ("Queue Empty, exiting worker thread") self.deadthreadqueue.put(self) return else: #logger.info("Thread #%d idle shutdown" % (self.ident)) self.deadthreadqueue.put(self) return # Record start time so we get a sense of performance task_start_time = time.time() # _sprint("+++ {0}".format(entry.name)) #logger.info("+++ {0}".format(entry.name)) # do it! try: proc = subprocess.Popen(entry.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, *entry.args, **entry.kwargs) entry.returned_value = proc.communicate(input) entry.stdoutdata = entry.returned_value[0].decode('utf-8') entry.stderrdata = entry.returned_value[1].decode('utf-8') proc = None except Exception as e: # inform operator of the name of the task throwing the exception # also, intercept the traceback and send to stderr.write() to avoid interweaving of traceback lines from parallel threads error_message = "\n*** {0}\n{1}\n{2}\n".format( entry.name, entry.args, traceback.format_exc()) #logger.error(error_message) sys.stderr.write(error_message) entry.exception = e entry.stdoutdata = None entry.returned_value = None entry.returncode = -1 entry.stderrdata = None # calculate finishing time and mark task as completed task_end_time = time.time() t_delta = task_end_time - task_start_time # mark the object event as completed entry.completed.set() # generate the time elapsed string for output seconds = math.fmod(t_delta, 60) seconds_str = "%02.5g" % seconds time_str = str(time.strftime('%H:%M:', time.gmtime(t_delta))) + seconds_str # print the completion notice with times aligned time_position = 70 out_string = "--- {0}".format(entry.name) out_string += " " * (time_position - len(out_string)) out_string += time_str #logging.info(out_string) # _sprint (out_string) JobsQueued = self.tasks.qsize() if JobsQueued > 0: JobQText = "Jobs Queued: " + str(self.tasks.qsize()) JobQText = ('\b' * 40) + JobQText + (' ' * (40 - len(JobQText))) nornir_pools._PrintProgressUpdate(JobQText) self.tasks.task_done()
def PrintActiveTaskCount(self): JobQText = "Jobs Queued: " + str(self.num_active_tasks) JobQText = ('\b' * 40) + JobQText + ('.' * (40 - len(JobQText))) nornir_pools._PrintProgressUpdate (JobQText) return