Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-polling/Non-blocking Timer?

Tags:

python

timer

The best solution I've found so far is to just use the sleep() function. I'd like to run my own callback function when the event of a timer expiration happens. Is there any event-driven way to go about it?

from time import sleep

# Sleep for a minute
time.sleep(60)
like image 408
tarabyte Avatar asked Mar 04 '14 19:03

tarabyte


3 Answers

There's a built-in simple solution, using the threading module:

import threading

timer = threading.Timer(60.0, callback)
timer.start()  # after 60 seconds, 'callback' will be called

## (in the meanwhile you can do other stuff...)

You can also pass args and kwargs to your callback. See here.

like image 78
Bach Avatar answered Oct 03 '22 08:10

Bach


I think it could be really simple. Take a look at this example. It works even in a python console!

from threading import Thread
from time import sleep

# Function to be called when the timer expires
def myFunction():
    print 'Did anyone call me?'

# Function with the timer
def myTimer(seconds):
    sleep(seconds)
    myFunction()

# Thread that will sleep in background and call your function
# when the timer expires.
myThread = Thread(target=myTimer, args=(4,))
myThread.start()

Put whatever amount of seconds you want, and keep working with the console or running the main thread/programm. You will notice that the function will be called when the timer comes to an end.

Edit

Another good example, considering the comment from @tarabyte is the one where the function is called only depending on the value of some variable or flag. I hope this would then be the answer @tarabyte is looking for.

from threading import Thread
from time import sleep

myFlag = False

# Function to be called when the flag turns on
def myFunction():
    print 'Did anyone call me?'

def myTimer():
    global myFlag
    while True:
        if myFlag:
            myFunction()
            myFlag = False
        else:
            sleep(1)

# Thread that will sleep in background and call your function
# when the myFlag turns to be True
myThread = Thread(target=myTimer)
myThread.start()

# Then, you can do whatever you want and later change the value of myFlag.
# Take a look at the output inside ipython when the value of myFlag is changed.


In [35]: myFlag
Out[35]: False

In [36]: myFlag = True

In [37]: Did anyone call me?
like image 33
Javier Avatar answered Oct 03 '22 08:10

Javier


Sometimes a simple solution is best, even if it polls the time. I have used this to great success before - it doesn't block if your thread doesn't stop on it.

I think I would manage this most simply by checking times, since this is so much more simple and resource economical than working out a separate threaded solution:

def event_minute_later(event):
    print(time.time()) # use for testing, comment out or delete for production
    return event + 60 < time.time()

And usage:

>>> event = time.time()
>>> print(event)
1393962502.62

>>> event_minute_later(event)
1393962526.73
False
>>> event_minute_later(event)
1393962562.9
True
like image 22
Russia Must Remove Putin Avatar answered Oct 03 '22 09:10

Russia Must Remove Putin