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
  "SyntaxError: invalid syntax" running code in Doing Math With Python b saucerdesigner 2 240 Nov-03-2020, 04:23 PM
Last Post: saucerdesigner
  Running code online aqua_ed 1 175 Oct-29-2020, 03:04 AM
Last Post: Larz60+
  Class function is not running at all even though it is clearly run in the code SheeppOSU 2 312 Sep-26-2020, 10:54 PM
Last Post: SheeppOSU
  Question re: running code in Editor (i.e. Atom) vs Python interpreter rok 3 504 Jun-29-2020, 09:20 PM
Last Post: bowlofred
  Modify code from running program ? samuelbachorik 2 476 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik
  Multiproccessing ValueError: Pool not running when running parallel functions Gilush 1 599 Jun-06-2020, 12:49 AM
Last Post: Gilush
  Code import .CSV file to MySQL table rtakle 4 637 Apr-30-2020, 03:16 PM
Last Post: anbu23
  How do you take terminal inputs w/o halting running code? Bhoot 3 505 Apr-17-2020, 08:31 AM
Last Post: deanhystad
  Error Message Coming Up When Running Code eddywinch82 0 602 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  Error in python code 'import callbot' Lou_Reed 3 666 Jan-07-2020, 09:33 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

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