Python Forum
code keeps running if i use from threading import timer? - 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: code keeps running if i use from threading import timer? (/thread-15629.html)



code keeps running if i use from threading import timer? - birddseedd - Jan-25-2019

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()



RE: code keeps running if i use from threading import timer? - stullis - Jan-25-2019

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.


RE: code keeps running if i use from threading import timer? - birddseedd - Jan-25-2019

(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


RE: code keeps running if i use from threading import timer? - stullis - Jan-25-2019

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.