Python Forum

Full Version: Interrupt/Break Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All,
I have been working on a class project, which is due tomorrow and I completed the project but I misinterpreted a requirement. I need a function which only runs for a certain given amount of time. Lets say 60seconds. I generate a while loop, that is dependent on the timer function and when 60seconds has expired the loop breaks, however this break function does not propagate into other called modules, nested whiles, and if loops. I am not sure if there is a better way to do this but I don’t have too much experience with Python and I am not aware of other options with handling this in Python. Any help would be appreciated. Here is the simplified structure of my python script, also if there is an option to break in the middle of the module would the return variables still be returned? I have tried it a couple of ways below is the latest of my attempts. 

Class Function 2: 
        _init_ program
     def module1: 
          code 
          code
    def module2: 
         code
         code
    def module 3: 
         code
         code
    def time_module:  
         while True:
                 code
                 code
                 code
                 While
                        Code
                        If
                        Code
                        Code
                  module3()
                 code
                 module2()
           if b >= 60: break
          code
          finish
main:
Function2()
time.sleep() in a thread? Or you can try this
It somewhat depends on how savagely you can kill what is currently running, and/or how often your timer loop gets control. You are using a rather "soft" method which could be adequate in some cases.

The "hard" method would be to spin off a thread to do the work, while the main thead just sleeps 60 seconds. When it wakes up, it kills the other thread and exits. Killing a thread is explained here