def scheduleTimeout(self, timeout_func, time_to_occur=0, frequency=0, arg_tuple=None, arg_dict=None): ''' Method used to schedule timeouts. Parameters: - timeout_func: a function specified by the developer to be invoked when each timeout occurs. - time_to_occur: the time (relative to this invocation) in which the first invocation of timeout_func will occur. This is in 100ns units and must be a postive integer value. A value of 0 denotes that timeout_func shoud be invoked immediately. - frequency: the frequency at which timeout_func will be invoked. This is in 100ns units and must be a postive integer value. A value of 0 implies the timeout is a "one shot" deal. - arg_tuple: tuple of values supplied to timeout_func. A deepcopy of this tuple is made - arg_dict: dictionary of values supplied to timeout_func. A deepcopy of this dictionary is made Returns: ID of the newly created timeout Raises: ??? ''' #increment the timeout ID to be returned by this method to guarantee #it's uniqueness self.timeout_counter += 1 timeout_id = self.timeout_counter #determine the first real execution date by adding the relative time #to the real current time. time_to_occur += getEpochTimeStamp().value #setup some parameters if arg_tuple == None: arg_tuple = () if arg_dict == None: arg_dict = {} #add the ID to the list self.lock.acquire() self.timeout_dict[timeout_id] = { 'time_to_occur': long(time_to_occur), 'frequency': long(frequency), 'timeout_func': timeout_func, 'is_suspended': 0, 'arg_tuple': deepcopy(arg_tuple), 'arg_dict': deepcopy(arg_dict) } self.lock.release() self.logger.logDebug("Timeout scheduled to occur:" + str(self.timeout_dict[timeout_id])) return timeout_id
def scheduleTimeout(self, timeout_func, time_to_occur = 0, frequency = 0, arg_tuple = None, arg_dict = None): ''' Method used to schedule timeouts. Parameters: - timeout_func: a function specified by the developer to be invoked when each timeout occurs. - time_to_occur: the time (relative to this invocation) in which the first invocation of timeout_func will occur. This is in 100ns units and must be a postive integer value. A value of 0 denotes that timeout_func shoud be invoked immediately. - frequency: the frequency at which timeout_func will be invoked. This is in 100ns units and must be a postive integer value. A value of 0 implies the timeout is a "one shot" deal. - arg_tuple: tuple of values supplied to timeout_func. A deepcopy of this tuple is made - arg_dict: dictionary of values supplied to timeout_func. A deepcopy of this dictionary is made Returns: ID of the newly created timeout Raises: ??? ''' #increment the timeout ID to be returned by this method to guarantee #it's uniqueness self.timeout_counter += 1 timeout_id = self.timeout_counter #determine the first real execution date by adding the relative time #to the real current time. time_to_occur += getEpochTimeStamp().value #setup some parameters if arg_tuple == None: arg_tuple = () if arg_dict == None: arg_dict = {} #add the ID to the list self.lock.acquire() self.timeout_dict[timeout_id] = {'time_to_occur':long(time_to_occur), 'frequency': long(frequency), 'timeout_func':timeout_func, 'is_suspended':0, 'arg_tuple':deepcopy(arg_tuple), 'arg_dict':deepcopy(arg_dict)} self.lock.release() self.logger.logDebug("Timeout scheduled to occur:" + str(self.timeout_dict[timeout_id])) return timeout_id
def __timeoutExecutor(self): ''' Utility function executed by a thread and actually calls timeouts. User code should never invoke this directly. ''' #continuously loop until this scheduler object is deleted while self.alive: #sleep first ACSSleep(self.minimum_sleep) #iterate through the entire list for timeout_id in self.timeout_dict.keys(): #use the lock to obtain all the info we can about the #timeout self.lock.acquire() try: time_to_occur = self.timeout_dict[timeout_id]['time_to_occur'] frequency = self.timeout_dict[timeout_id]['frequency'] timeout_func = self.timeout_dict[timeout_id]['timeout_func'] arg_tuple = self.timeout_dict[timeout_id]['arg_tuple'] arg_dict = self.timeout_dict[timeout_id]['arg_dict'] is_suspended = self.timeout_dict[timeout_id]['is_suspended'] self.lock.release() except: #it's possible that the user ran Scheduler.cancelAllTimeouts() #and the timeoutID no longer exists. as a result #sometimes it might be necessary to skip it self.lock.release() continue #check to see if the deadline has not passed and will not pass #before the next round now = getEpochTimeStamp().value if (time_to_occur>now)and(time_to_occur > (now+self.minimum_sleep)): #OK to skip this timeout until the next round continue #if it's suspended, we just move on to the next timeout if is_suspended: continue #if we've gotten this far, it's OK to execute the timeout try: timeout_func(*arg_tuple, **arg_dict) except: self.logger.logCritical("Failed to invoke function denoted by ID:" + str(timeout_id)) print_exc() #check to see if it was a one-time ordeal if frequency == 0L: #need to delete this timeout ID self.cancelTimeout(timeout_id) #figure out when the next invocation will be else: next_time = getEpochTimeStamp().value + frequency self.lock.acquire() try: self.timeout_dict[timeout_id]['time_to_occur'] = next_time except: #it's possible that the user ran Scheduler.cancelAllTimeouts() #and the timeout_id no longer exists. as a result #sometimes it might be necessary to ignore this error pass self.lock.release()
def __timeoutExecutor(self): ''' Utility function executed by a thread and actually calls timeouts. User code should never invoke this directly. ''' #continuously loop until this scheduler object is deleted while self.alive: #sleep first ACSSleep(self.minimum_sleep) #iterate through the entire list for timeout_id in self.timeout_dict.keys(): #use the lock to obtain all the info we can about the #timeout self.lock.acquire() try: time_to_occur = self.timeout_dict[timeout_id][ 'time_to_occur'] frequency = self.timeout_dict[timeout_id]['frequency'] timeout_func = self.timeout_dict[timeout_id][ 'timeout_func'] arg_tuple = self.timeout_dict[timeout_id]['arg_tuple'] arg_dict = self.timeout_dict[timeout_id]['arg_dict'] is_suspended = self.timeout_dict[timeout_id][ 'is_suspended'] self.lock.release() except: #it's possible that the user ran Scheduler.cancelAllTimeouts() #and the timeoutID no longer exists. as a result #sometimes it might be necessary to skip it self.lock.release() continue #check to see if the deadline has not passed and will not pass #before the next round now = getEpochTimeStamp().value if (time_to_occur > now) and (time_to_occur > (now + self.minimum_sleep)): #OK to skip this timeout until the next round continue #if it's suspended, we just move on to the next timeout if is_suspended: continue #if we've gotten this far, it's OK to execute the timeout try: timeout_func(*arg_tuple, **arg_dict) except: self.logger.logCritical( "Failed to invoke function denoted by ID:" + str(timeout_id)) print_exc() #check to see if it was a one-time ordeal if frequency == 0L: #need to delete this timeout ID self.cancelTimeout(timeout_id) #figure out when the next invocation will be else: next_time = getEpochTimeStamp().value + frequency self.lock.acquire() try: self.timeout_dict[timeout_id][ 'time_to_occur'] = next_time except: #it's possible that the user ran Scheduler.cancelAllTimeouts() #and the timeout_id no longer exists. as a result #sometimes it might be necessary to ignore this error pass self.lock.release()