1

In Python I would like to run a function calling a default action for 20 seconds. However, there are 5 specific timings within these 20 seconds when another function should be triggered. In order to simplify my code, I have replaced the "action" functions with simple printing commands.

Here is what I have so far - The output seems ok, in that it lasts for 10 seconds and prints the time and the default state/action. But the triggered action is missing! Is there a better/correct way to do this?

import random
import time
import numpy as np
import itertools

def uniform_min_range(a, b, n, min_dist):
    while True:
        atimes = np.random.uniform(a, b, size=n)
        np.sort(atimes)
        if np.all(np.diff(atimes) >= min_dist):
            return atimes

def timings():
    global times
    times = uniform_min_range(0, 20, 5, 1.0) 
    print 'beep times: ', times

def defaultAction():
    global start
    print 'wobble'

def triggeredAction():
    global start
    print 'actionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn'

def main():
    global times
    timings()
    print 'beep times: ', times
    start = time.time()
    t_end = time.time() + 20   #### end after 20 seconds
    while time.time() < t_end: #### for 20 sec/ until end reached
        print str(time.time()-start)
        if (time.time()-start) == times[0] or (time.time()-start) == times[1] or (time.time()-start) == times[2] or (time.time()-start) == times[3]:
            triggeredAction()
        elif (time.time()-start) != times[0] or (time.time()-start) != times[1] or (time.time()-start) != times[2] or (time.time()-start) != times[3]:
            defaultAction()

    print "END"

main()
Spica
  • 127
  • 2
  • 10

1 Answers1

0

time.time() returns second since epoch as a floating point number like:

>>> import time
>>> time.time()
1465572505.891256

If your comparison:

time.time()-start) == times[0]

to time.time() isn't correct down to the microsecond (this depends on the system), then the == won't be True and you'll never get your triggeredAction().

Do things by the second if that works for you, use: int(time.time()) and do the same for your uniform_min_range return values - assuming per second resolution is ok for you. Otherwise you'll need to introduce a range (+ or - 1s) to your triggered action check.

RedCraig
  • 503
  • 1
  • 4
  • 15
  • Thank you RedCraig! It worked, but the sampling resolution seems higher than at the level of the second. This means that second 1 is repeated for a whole lot of times. Then second two etc. Do you know why this happens? – Spica Jun 10 '16 at 16:15
  • The reason why I am asking is that if second 2 should trigger the action and second two is repeated, then the action will be triggered multiple times successively. Ideally the "sampling rate" should also be every second. – Spica Jun 10 '16 at 16:17
  • Well, the number of times it'll get sampled per second depends on the number of iterations of your while loop your CPU can do in a second. If you want the sampling rate to be once per second you can add code to check it manually. Instead though, I'd use a [`Threading.Timer`](https://docs.python.org/2/library/threading.html#timer-objects) object instead. Move the `while` loop into a method, schedule the method with a Timer, and in the method reschedule the method to run again in another second. Like [this](http://stackoverflow.com/a/3393759/2175385) answer does. – RedCraig Jun 10 '16 at 16:38
  • Thank you RedCraig, I'll try that! – Spica Jun 10 '16 at 16:41