Interrupt/Break Function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Interrupt/Break Function (/thread-2072.html) |
Interrupt/Break Function - sdprelude - Feb-16-2017 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() RE: Interrupt/Break Function - wavic - Feb-17-2017 time.sleep() in a thread? Or you can try this RE: Interrupt/Break Function - Ofnuts - Feb-17-2017 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 |