Python threading.Timer 只重复一次 [英] Python threading.Timer only repeats once

查看:29
本文介绍了Python threading.Timer 只重复一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()
    .
    .
    .

如您所见,我尝试在调用 StartAnimation 函数后每 2 秒重复一次 ExecuteDemo() 函数.但我的问题是我的 ExecuteDemo() 只运行了两次.我怎样才能让它不断重复?我错过了什么吗?

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?

推荐答案

来自文档:

类 threading.Timer

class threading.Timer

在指定的时间间隔过后执行函数的线程.

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

这意味着 Threading.Timer 将在指定的时间段之后调用一个函数.正如您所注意到的,它只会被调用一次.这里的解决方案是在 ExecuteDemo(..) 函数的末尾再次设置计时器.

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()


在我看来,上面的方法有点低效.就像每 2 秒创建一个新线程,一旦执行该函数,它就会在创建下一个线程之前死亡.


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.

我会建议这样的:

def ExecuteDemoCaller():
    #while True: # or something..
    while someCondition:
        ExecuteDemo()
        time.sleep(2)

这篇关于Python threading.Timer 只重复一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆