January 30th, 2013, 03:12 AM
Run a function for X seconds
My name is Radu and I am from Romania.
I have a problem with a coundown routine.
My program basicaly looks like this :
while True :
If PLAY pressed...
if NEXT pressed...
So, when i press the PLAY button i need to run a function for a specific time (like 10 seconds), BUT in this time a may press NEXT, so i can't use time.sleep(seconds).
How can i make this run ?
Thank you !
January 30th, 2013, 09:03 AM
run the function in a separate thread or process.
Then kill that process on appropriate event.
One of the events is the 10 second timer,
Various buttons could preempt the timer.
[/code] are essential for python code and Makefiles!
January 30th, 2013, 11:06 AM
Assuming you are using Wx for the GUI, use timer() (many examples on the web) to call some function that will set a variable causing the function to exit. Note that Wx also has threads.
January 30th, 2013, 11:09 AM
Thank you for your replays !
I dont't have a GUI ....I have a 2x16 LCD.
The buttons are (hardware) push-buttons.
I can't figure out the algorithm
January 30th, 2013, 02:21 PM
Then multiprocessing/threading is the only option since you want to do 2 things at the same time, keep track of the time, and check for button presses. A simple example where the process is terminated after 5 seconds and the list's value is changed to simulate a button press or whatever, which could also return/exit from the function.
from multiprocessing import Process
self.ctr = 0
def test_f(self, name):
self.ctr += 1
print "test_f", self.ctr, name, self.ctr_list
if __name__ == '__main__':
p = Process(target=CT.test_f, args=('P',))
print "ctr_list", CT.ctr_list
## sleep for 5 seconds and terminate