timer

How to check if a Threading.Timer object is currently running in python

How to check if a Threading.Timer object is currently running in python Question: let’s say i defined a timer like: def printer(data): print data data= “hello” timer_obj = Timer(5,printer,args=[data]) timer_obj.start() # some code if( #someway to check timer object is currently ticking): #do something So is there a way that if the timer object is …

Total answers: 2

Alternative of threading.Timer?

Alternative of threading.Timer? Question: I have a producer-consumer pattern Queue, it consumes incoming events and schedule qualified events sending out in 5 seconds. I am using threading.Timer()python documentto do it and everything was working fine. Recently, I was requested to change the scheduled time from 5 second to 30 minutes, and threading.Timer() crashes my script …

Total answers: 1

Python loop to run for certain amount of seconds

Python loop to run for certain amount of seconds Question: I have a while loop, and I want it to keep running through for 15 minutes. it is currently: while True: #blah blah blah (this runs through, and then restarts. I need it to continue doing this except after 15 minutes it exits the loop) …

Total answers: 7

How can I reproduce the race conditions in this python code reliably?

How can I reproduce the race conditions in this python code reliably? Question: Context I recently posted a timer class for review on Code Review. I’d had a gut feeling there were concurrency bugs as I’d once seen 1 unit test fail, but was unable to reproduce the failure. Hence my post to code review. …

Total answers: 4

Time-Limited Input?

Time-Limited Input? Question: What I would like to be able to do is ask a user a question using input. For example: print(‘some scenario’) prompt = input(“You have 10 seconds to choose the correct answer…n”) and then if the time elapses print something like print(‘Sorry, times up.’) Any help pointing me in the right direction …

Total answers: 2

threading.Timer – repeat function every 'n' seconds

threading.Timer – repeat function every 'n' seconds Question: I want to fire off a function every 0.5 seconds and be able to start and stop and reset the timer. I’m not too knowledgeable of how Python threads work and am having difficulties with the python timer. However, I keep getting RuntimeError: threads can only be …

Total answers: 16

Executing periodic actions

Executing periodic actions Question: I am working on Windows. I want to execute a function foo() every 10 seconds. How do I do this? Asked By: Bruce || Source Answers: This will insert a 10 second sleep in between every call to foo(), which is approximately what you asked for should the call complete quickly. …

Total answers: 9

how to pass parameters of a function when using timeit.Timer()

how to pass parameters of a function when using timeit.Timer() Question: This is the outline of a simple program # some pre-defined constants A = 1 B = 2 # function that does something critical def foo(num1, num2): # do something # main program…. do something to A and B for i in range(20): # …

Total answers: 12

python theading.Timer: how to pass argument to the callback?

python theading.Timer: how to pass argument to the callback? Question: I tried this code: import threading def hello(arg, kargs): print(arg) t = threading.Timer(2, hello, "bb") t.start() while 1: pass The output is just b. How can I pass arguments to the callback properly? If I remove the kargs parameter from hello, I get an exception …

Total answers: 2

What is the best way to repeatedly execute a function every x seconds?

How to repeatedly execute a function every x seconds? Question: I want to repeatedly execute a function in Python every 60 seconds forever (just like an NSTimer in Objective C or setTimeout in JS). This code will run as a daemon and is effectively like calling the python script every minute using a cron, but …

Total answers: 22