【问题标题】:Repeat function at an interval?间隔重复功能?
【发布时间】:2013-02-19 22:33:13
【问题描述】:

我正在制作一个 wxPython 应用程序,我需要每 15 秒从 Internet 更新一个值。有什么方法可以让我有一个函数来设置值,并让它在这个时间间隔在后台运行,而不会中断程序?

编辑:这是我正在尝试的:

import thread

class UpdateThread(Thread):
    def __init__(self):
        self.stopped = False
        UpdateThread.__init__(self)
    def run(self):
        while not self.stopped:
            downloadValue()
            time.sleep(15)
def downloadValue():
    print x

UpdateThread.__init__()

【问题讨论】:

    标签: python multithreading timer wxpython


    【解决方案1】:

    你想要的是添加一个线程以指定的速度运行你的任务。

    您可以在这里查看这个很棒的答案:https://stackoverflow.com/a/12435256/667433 以帮助您实现这一目标。

    编辑:这是应该为您工作的代码:

    import time
    from threading import Thread # This is the right package name
    
    class UpdateThread(Thread):
        def __init__(self):
            self.stopped = False
            Thread.__init__(self) # Call the super construcor (Thread's one)
        def run(self):
            while not self.stopped:
                self.downloadValue()
                time.sleep(15)
        def downloadValue(self):
            print "Hello"
    
    myThread = UpdateThread()
    myThread.start()
    
    for i in range(10):
        print "MainThread"
        time.sleep(2)
    

    希望对你有帮助

    【讨论】:

    • 所以我已经完成了课程,那么你如何开始呢?当我启动它时,我会收到NameError: name 'Thread' is not defined
    • 搞定了。那么,一旦它启动,它会在同一个终端中独立运行,但与脚本的主要流程是分开的?
    • 是的,完全正确。为了说服自己,您可以在myThread.start() 行之后添加一段代码(如答案所示)
    • 是的,只需将myThread.stopped 设置为True,它最多会在 15 秒后结束。您可以通过在主线程中调用myThread.join() 函数来了解它,该函数将阻塞直到线程完成。
    • 没错,或者你也可以把它放在你的OnExit事件处理程序中。
    【解决方案2】:

    我做了类似的东西:

    -你需要一个线程在后台运行。

    -并定义一个“自定义”事件,以便在需要时可以通知 UI

    创建自定义 WX 事件

    (MyEVENT_CHECKSERVER, EVT_MYEVENT_CHECKSERVER) = wx.lib.newevent.NewEvent()

    在 UI "init" 上可以绑定事件,并启动线程

        #  bind the custom event 
        self.Bind(EVT_MYEVENT_CHECKSERVER, self.foo)
        # and start the worker thread
        checkServerThread = threading.Thread(target=worker_checkServerStatus
                                            ,args=(self,) )
        checkServerThread.daemon = True
        checkServerThread.start()
    

    工作线程可以是这样的,ps。 caller 是 UI 实例

    def worker_checkServerStatus(调用者):

       while True:    
           # check the internet code here
           evt = MyEVENT_CHECKSERVER(status='Some internet Status' ) #make a new event
           wx.PostEvent(caller, evt) # send the event to the UI
           time.sleep(15) #ZZZzz for a bit
    

    编辑:错过阅读问题...

    【讨论】:

      【解决方案3】:

      另一种方法是使用计时器:

      import threading
      stopNow = 0
      def downloadValue():
          print("Running downloadValue")
          if not stopNow: threading.Timer(15,downloadValue).start()
      
      downloadValue()
      

      这是重复函数的经典模式:函数本身会为其自身添加一个定时调用。要开始循环,请调用该函数(它立即返回)。要打破循环,请将 stopNow 设置为 1。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-30
        • 2014-01-31
        • 1970-01-01
        相关资源
        最近更新 更多