def run( self ): """ task execution reads and executes ProcessTask :task: out of pending queue and then pushes it to the results queue for callback execution :param self: self reference """ ## start watchdog thread print "PID:%s PPID:%s Started WorkingProcess...." % (os.getpid(), os.getppid()) self.__watchdogThread = threading.Thread( target = self.__watchdog ) self.__watchdogThread.daemon = True self.__watchdogThread.start() print "PID:%s PPID:%s started watchdogThread.." % (os.getpid(), os.getppid()) ## http://cdn.memegenerator.net/instances/400x/19450565.jpg if LockRing: # Reset all locks lr = LockRing() lr._openAll() lr._setAllEvents() ## zero processed task counter taskCounter = 0 ## zero idle loop counter idleLoopCount = 0 ## main loop while True: ## draining, stopEvent is set, exiting if self.__stopEvent.is_set(): return ## clear task self.task = None ## read from queue try: print "PID:%s PPID:%s Will try to GET from Pending Queue..." % (os.getpid(), os.getppid()) task = self.__pendingQueue.get( block = True, timeout = 5 ) # timeout changed from 10 print "PID:%s PPID:%s GOT a task from pending queue.." % (os.getpid(), os.getppid()) except Queue.Empty: ## idle loop? idleLoopCount += 1 ## 10th idle loop - exit, nothing to do if idleLoopCount == 2: # changed from 10 print "PID:%s PPID:%s Tried 10 times to get something, exiting..."% (os.getpid(), os.getppid()) return continue ## toggle __working flag self.__working.value = 1 ## save task self.task = task ## reset idle loop counter idleLoopCount = 0 ## process task in a separate thread self.__processThread = threading.Thread( target = self.__processTask ) self.__processThread.start() print "PID:%s PPID:%s started process Thread..." % (os.getpid(), os.getppid()) ## join processThread with or without timeout if self.task.getTimeOut(): print "WILL WAIT FOR JOIN(timeout)" self.__processThread.join( self.task.getTimeOut()+10 ) else: print "WILL WAIT FOR JOIN" self.__processThread.join() ## processThread is still alive? stop it! if self.__processThread.is_alive(): self.__processThread._Thread__stop() print "MUST FORCE-STOP PROCESS THREAD" print "PID:%s PPID:%s Process thread done..."% (os.getpid(), os.getppid()) ## check results and callbacks presence, put task to results queue if self.task.hasCallback() or self.task.hasPoolCallback(): if not self.task.taskResults() and not self.task.taskException(): self.task.setResult( S_ERROR("Timed out") ) print ">>>TIMED OUT!!!!" self.__resultsQueue.put( task ) print "ResultsQueue = %s " % self.__resultsQueue.qsize() ## increase task counter taskCounter += 1 self.__taskCounter = taskCounter ## toggle __working flag self.__working.value = 0
def run(self): """ task execution reads and executes ProcessTask :task: out of pending queue and then pushes it to the results queue for callback execution :param self: self reference """ ## start watchdog thread self.__watchdogThread = threading.Thread(target=self.__watchdog) self.__watchdogThread.daemon = True self.__watchdogThread.start() ## http://cdn.memegenerator.net/instances/400x/19450565.jpg if LockRing: # Reset all locks lr = LockRing() lr._openAll() lr._setAllEvents() ## zero processed task counter taskCounter = 0 ## zero idle loop counter idleLoopCount = 0 ## main loop while True: ## draining, stopEvent is set, exiting if self.__stopEvent.is_set(): return ## clear task self.task = None ## read from queue try: task = self.__pendingQueue.get(block=True, timeout=10) except Queue.Empty: ## idle loop? idleLoopCount += 1 ## 10th idle loop - exit, nothing to do if idleLoopCount == 10: return continue ## toggle __working flag self.__working.value = 1 ## save task self.task = task ## reset idle loop counter idleLoopCount = 0 ## process task in a separate thread self.__processThread = threading.Thread(target=self.__processTask) self.__processThread.start() ## join processThread with or without timeout if self.task.getTimeOut(): self.__processThread.join(self.task.getTimeOut() + 10) else: self.__processThread.join() ## processThread is still alive? stop it! if self.__processThread.is_alive(): self.__processThread._Thread__stop() ## check results and callbacks presence, put task to results queue if self.task.hasCallback() or self.task.hasPoolCallback(): if not self.task.taskResults() and not self.task.taskException( ): self.task.setResult(S_ERROR("Timed out")) self.__resultsQueue.put(task) ## increase task counter taskCounter += 1 self.__taskCounter = taskCounter ## toggle __working flag self.__working.value = 0
def run(self): """ Task execution Reads and executes ProcessTask :task: out of pending queue and then pushes it to the results queue for callback execution. :param self: self reference """ # # start watchdog thread self.__watchdogThread = threading.Thread(target=self.__watchdog) self.__watchdogThread.daemon = True self.__watchdogThread.start() # # http://cdn.memegenerator.net/instances/400x/19450565.jpg if LockRing: # Reset all locks lr = LockRing() lr._openAll() lr._setAllEvents() # # zero processed task counter taskCounter = 0 # # zero idle loop counter idleLoopCount = 0 # # main loop while True: # # draining, stopEvent is set, exiting if self.__stopEvent.is_set(): return # # clear task self.task = None # # read from queue try: task = self.__pendingQueue.get(block=True, timeout=10) except Queue.Empty: # # idle loop? idleLoopCount += 1 # # 10th idle loop - exit, nothing to do if idleLoopCount == 10 and not self.__keepRunning: return continue # # toggle __working flag self.__working.value = 1 # # save task self.task = task # # reset idle loop counter idleLoopCount = 0 # # process task in a separate thread self.__processThread = threading.Thread(target=self.__processTask) self.__processThread.start() timeout = False noResults = False # # join processThread with or without timeout if self.task.getTimeOut(): self.__processThread.join(self.task.getTimeOut() + 10) else: self.__processThread.join() # # processThread is still alive? stop it! if self.__processThread.is_alive(): self.__processThread._Thread__stop() self.task.setResult(S_ERROR(errno.ETIME, "Timed out")) timeout = True # if the task finished with no results, something bad happened, e.g. # undetected timeout if not self.task.taskResults() and not self.task.taskException(): self.task.setResult(S_ERROR("Task produced no results")) noResults = True # # check results and callbacks presence, put task to results queue if self.task.hasCallback() or self.task.hasPoolCallback(): self.__resultsQueue.put(task) if timeout or noResults: # The task execution timed out, stop the process to prevent it from running # in the background time.sleep(1) os.kill(self.pid, signal.SIGKILL) return # # increase task counter taskCounter += 1 self.__taskCounter = taskCounter # # toggle __working flag self.__working.value = 0
def run( self ): """ task execution reads and executes ProcessTask :task: out of pending queue and then pushes it to the results queue for callback execution :param self: self reference """ ## start watchdog thread self.__watchdogThread = threading.Thread( target = self.__watchdog ) self.__watchdogThread.daemon = True self.__watchdogThread.start() ## http://cdn.memegenerator.net/instances/400x/19450565.jpg if LockRing: # Reset all locks lr = LockRing() lr._openAll() lr._setAllEvents() ## zero processed task counter taskCounter = 0 ## zero idle loop counter idleLoopCount = 0 ## main loop while True: ## draining, stopEvent is set, exiting if self.__stopEvent.is_set(): return ## clear task self.task = None ## read from queue try: task = self.__pendingQueue.get( block = True, timeout = 10 ) except Queue.Empty: ## idle loop? idleLoopCount += 1 ## 10th idle loop - exit, nothing to do if idleLoopCount == 10: return continue ## toggle __working flag self.__working.value = 1 ## save task self.task = task ## reset idle loop counter idleLoopCount = 0 ## process task in a separate thread self.__processThread = threading.Thread( target = self.__processTask ) self.__processThread.start() ## join processThread with or without timeout if self.task.getTimeOut(): self.__processThread.join( self.task.getTimeOut()+10 ) else: self.__processThread.join() ## processThread is still alive? stop it! if self.__processThread.is_alive(): self.__processThread._Thread__stop() ## check results and callbacks presence, put task to results queue if self.task.hasCallback() or self.task.hasPoolCallback(): if not self.task.taskResults() and not self.task.taskException(): self.task.setResult( S_ERROR("Timed out") ) self.__resultsQueue.put( task ) ## increase task counter taskCounter += 1 self.__taskCounter = taskCounter ## toggle __working flag self.__working.value = 0
def run( self ): """ Task execution Reads and executes ProcessTask :task: out of pending queue and then pushes it to the results queue for callback execution. :param self: self reference """ ## start watchdog thread self.__watchdogThread = threading.Thread( target = self.__watchdog ) self.__watchdogThread.daemon = True self.__watchdogThread.start() ## http://cdn.memegenerator.net/instances/400x/19450565.jpg if LockRing: # Reset all locks lr = LockRing() lr._openAll() lr._setAllEvents() ## zero processed task counter taskCounter = 0 ## zero idle loop counter idleLoopCount = 0 ## main loop while True: ## draining, stopEvent is set, exiting if self.__stopEvent.is_set(): return ## clear task self.task = None ## read from queue try: task = self.__pendingQueue.get( block = True, timeout = 10 ) except Queue.Empty: ## idle loop? idleLoopCount += 1 ## 10th idle loop - exit, nothing to do if idleLoopCount == 10 and not self.__keepRunning: return continue ## toggle __working flag self.__working.value = 1 ## save task self.task = task ## reset idle loop counter idleLoopCount = 0 ## process task in a separate thread self.__processThread = threading.Thread( target = self.__processTask ) self.__processThread.start() timeout = False noResults = False ## join processThread with or without timeout if self.task.getTimeOut(): self.__processThread.join( self.task.getTimeOut()+10 ) else: self.__processThread.join() ## processThread is still alive? stop it! if self.__processThread.is_alive(): self.__processThread._Thread__stop() self.task.setResult( S_ERROR( errno.ETIME, "Timed out" ) ) timeout = True # if the task finished with no results, something bad happened, e.g. # undetected timeout if not self.task.taskResults() and not self.task.taskException(): self.task.setResult( S_ERROR("Task produced no results") ) noResults = True ## check results and callbacks presence, put task to results queue if self.task.hasCallback() or self.task.hasPoolCallback(): self.__resultsQueue.put( task ) if timeout or noResults: # The task execution timed out, stop the process to prevent it from running # in the background time.sleep( 1 ) os.kill( self.pid, signal.SIGKILL ) return ## increase task counter taskCounter += 1 self.__taskCounter = taskCounter ## toggle __working flag self.__working.value = 0