#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Run a function for X seconds


    Hello,

    My name is Radu and I am from Romania.

    I have a problem with a coundown routine.
    My program basicaly looks like this :

    main():
    ...something...
    while True :
    if Button1_pressed....
    If Button2_pressed...
    If PLAY pressed...
    if NEXT pressed...
    etc

    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 !

    Regards,
    Radu
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    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]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    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.
    Code:
    import time
    from multiprocessing import Process
    
    class TestClass():
    
       def __init__(self):
          self.ctr = 0
          self.ctr_list=[0]
    
       def test_f(self, name):
          while True:
             self.ctr += 1
             print "test_f", self.ctr, name, self.ctr_list
             time.sleep(0.5)
    
    if __name__ == '__main__':
       CT=TestClass()
       p = Process(target=CT.test_f, args=('P',))
       p.start()
    
       time.sleep(2)
       CT.ctr_list[0]=10
       print "ctr_list", CT.ctr_list
    
       ## sleep for 5 seconds and terminate
       time.sleep(5.0)
       p.terminate()

IMN logo majestic logo threadwatch logo seochat tools logo