6
def On_Instrumentation_StartAnimation():  

    """
    Syntax      : On_Instrumentation_StartAnimation()
    Purpose     : Fired if the animation is started
    Parameters  : None
    """
    print "----------------------------------------------------------------------------------------"
    localtime = time.asctime(time.localtime(time.time()))
    global start
    start = time.clock()
    print "The user entered Animation Mode at local time: ", localtime
    print("This data has also been written to 'c:\dSPACE71\cdlog\cdlog.txt'")   
    threading.Timer(2, ExecuteDemo).start()
    ExecuteDemo()
    # Printing to the text file
    file1 = open('c:\dSPACE71\cdlog\cdlog.txt', 'a')
    file1.write("\n----------------------------------------------------------------------------------------")
    file1.write("\nThe user entered Animation Mode at local time: ")
    file1.write(localtime)
    file1.close()      

def ExecuteDemo()
    .
    .
    .
    Current_value = Current.Read()
    localtime = time.asctime(time.localtime(time.time()))
    print "The current reading at localtime:", localtime, "is", str(Current_value) + "."
    # Printing to the text file
    file1 = open('c:\dSPACE71\cdlog\cdlog.txt', 'a')
    file1.write("\n----------------------------------------------------------------------------------------")
    file1.write("\nThe current reading at localtime: ")
    file1.write(localtime)
    file1.write(" is: ")
    file1.write(str(Current_value))
    file1.close()
    .
    .
    .

As you can hopefully see, I'm trying to repeat the ExecuteDemo() function every 2 seconds after the StartAnimation function is called. But my problem here is that my ExecuteDemo() only runs twice. How can I get it to keep repeating? Am I missing something?

4 Answers 4

15

From the documentation:

class threading.Timer

A thread that executes a function after a specified interval has passed.

This means Threading.Timer will call a function after a specified period of time. And as you noticed, it gets called only once. The solution here will to have the timer set once again at the end of the ExecuteDemo(..) function.

def ExecuteDemo():
    .
    .
    .
    threading.Timer(2, ExecuteDemo).start()

In my opinion, the above method is a little inefficient. It is like every 2 seconds a new thread is being created, and once it executes the function, it dies before creating the next thread.

I would suggest something like this:

def ExecuteDemoCaller():
    #while True: # or something..
    while someCondition:
        ExecuteDemo()
        time.sleep(2)
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for the while True: approach. This is much more Pythonic in my opinion, and much more expressive of the intent, which is to loop indefinitely. while breakCondition: would also be meaningful, assuming that ExecuteDemo could change the value stored there.
@g.d.d.c The reason why I have # or something over there :)
+1 for the same reasons. However, it's worth pointing out that all of the solutions given have the same problem: it won't average out to 30 iterations/minute, it'll always be slower. The only way around that is to use a sleep-until function, with a timestamp that you always increment by 2 seconds.
PS, a really simple sleep-until function is "delay = t - time.time(); if delay > 0: sleep(delay)". Don't use this for 30fps animation or 20ms audio buffering, but for the OP's use case, it should be fine.
1

From the docs:

class threading.Timer(interval, function, args=[], kwargs={})¶ Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed.

I couldn't find anything about repeatedly calling something. Maybe someone else has a better answer, or maybe you have to do handroll it (which wouldn't be too hard).

Comments

1
class setInterval(threading.Thread):
 def __init__(self, interval, function, args=[], kwargs={}):
  threading.Thread.__init__(self)
  self.interval = interval
  self.function = function
  self.args = args
  self.kwargs = kwargs
  self.finished = threading.Event()
  self.flag_run = True
 def cancel(self):
  self.finished.set()
 def run(self):
  while self.flag_run:
   self.finished.wait(self.interval)
   if not self.finished.is_set(): self.function(*self.args, **self.kwargs)
   else: self.flag_run = False
  self.finished.set()

After 10 minutes reading of "threading.py" I use this code

Comments

0

Timer doesn't repeat. You can just set another timer at the end of your callback function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.