Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interrupt/Break Function
#1
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()
Reply
#2
time.sleep() in a thread? Or you can try this
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  add interrupt for next task kucingkembar 0 752 Oct-07-2022, 12:15 PM
Last Post: kucingkembar
  Determine if keyboard interrupt versus SIGINT trapped? Jibunnokage 5 1,707 Sep-30-2022, 06:54 AM
Last Post: Gribouillis
  Enabling interrupt on Adafruits button/led board Moris526 0 1,987 Apr-30-2021, 03:29 PM
Last Post: Moris526
  python delay without interrupt the whole code Nick_tkinter 4 5,054 Feb-22-2021, 10:51 PM
Last Post: nilamo
  Adafruits Neotrellis interrupt with RAsp and Python Moris526 5 3,484 Jan-01-2021, 11:43 PM
Last Post: Moris526
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 1,769 Dec-28-2020, 05:42 AM
Last Post: Moris526
  input interrupt Nickd12 1 4,181 Dec-09-2020, 05:01 PM
Last Post: Gribouillis
  interrupt serial port trainee1 9 18,748 Feb-15-2017, 08:19 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020