Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat function at an interval?

I'm making a wxPython app that I need to update a value from the internet every 15 seconds. Is there any way I can have a function to set the value, and make it run in the background at this interval, without interrupting the program?

EDIT: Here's what I'm trying:

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__()
like image 587
tkbx Avatar asked May 22 '26 10:05

tkbx


1 Answers

What you want is to add a thread that runs your task at a specified pace.

You may have a look at this great answer here : https://stackoverflow.com/a/12435256/667433 to help you achieve this.

EDIT : Here is the code that should work for you :

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)

Hope it helps

like image 199
Y__ Avatar answered May 24 '26 00:05

Y__