Python Forum
code keeps running if i use from threading import timer?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code keeps running if i use from threading import timer?
#1
seems like my code keeps running if i use this to create a pause. this isn't exactly my goal. I need to "pause" the code for a period of time. but if an "emergency stop" button is pressed during the pause id like to run a final line of code (which stops the machine) and halt all further operation of code.

thanks

########libraries########
import csv
from threading import Timer

########variables########
servo1pos = ''
servo2pos = ''
rtkgpspos = ''
compasspos = ''
delay_in_sec = 5

########Functions########
def countdelay(delay_in_sec):
    print("in function")
    
def yes():
    print("sure is")
    
########Main Code########
print("go")
t = Timer(delay_in_sec, countdelay, [delay_in_sec])  # countdelay function will be called 2 sec later with [delay_in_sec] as *args parameter
t.start()  # returns None
print("done")
print("is other code executing?")
yes()
Reply
#2
To halt execution of the main thread while the timer is running, call the Timer.join() method. This forces the calling thread to wait for the Timer to complete.

As for the cancel operation, I'm thinking that you'll need a second thread. The second thread would call a function requesting input. If input is given, it cancels the countdelay() thread; if no input is given and the countdelay thread is completed, the thread terminates.
Reply
#3
(Jan-25-2019, 04:26 AM)stullis Wrote: To halt execution of the main thread while the timer is running, call the Timer.join() method. This forces the calling thread to wait for the Timer to complete.

As for the cancel operation, I'm thinking that you'll need a second thread. The second thread would call a function requesting input. If input is given, it cancels the countdelay() thread; if no input is given and the countdelay thread is completed, the thread terminates.

Thank you for your reply. How do I create a new threat? This is my first time dealing with multiple threads
Reply
#4
Import Thread from the threading module and instantiate one.

from threading import Thread

x = Thread(func)
The function you pass in will need a parameter for the other thread so it can check the status of it. Frankly, the function will be the tricky part because it needs a time restricted input. To my knowledge, that would require yet another thread to terminate the input thread; that thread would just be a timer. Then again, I wonder if I'm over-complicating it and if there is a more straightforward way to do it.

In essence, you need to setup a race condition such that whichever thread completes first takes precedence. If the Timer completes first, then it calls its function. If the timed input thread completes first, it cancels the Timer altogether.

Where I'm getting stuck conceptually is with the input. User input halts code processing until input is provided. Since it's only valid if the Timer has not completed though, the thread requesting input needs to terminate if the Timer activates. Checking if the Timer has activated requires code to be running though.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 546 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 345 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 479 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,165 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,716 May-03-2023, 08:22 AM
Last Post: billykid999
  Why is this import running twice? extan1 10 2,713 Feb-17-2023, 02:02 AM
Last Post: extan1
  using threading.Timer for function korenron 1 1,155 Dec-20-2022, 01:09 PM
Last Post: ndc85430
  Code running many times nad not just one? korenron 4 1,325 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,548 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,421 Apr-06-2022, 03:41 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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