1
matches = []
done = []
for item in matches:
    dofunctioneveryloop()
    done.extent(item)
    dofunctiononce5min()

How can I execute dofunctiononce5min() inside this loop once 5 minute? This is backup to file function is this possible?

3
  • Do you want to execute that function every five minutes, or every five minutes while inside that loop? How long do you expect that loop to take? Commented Aug 6, 2014 at 19:45
  • What have you tried so far ? Do you want the method to wait 5 minutes and then do something or check that it has been 5 minutes since the last execution ? Commented Aug 6, 2014 at 19:45
  • Do I understand you correct, that you want to execute this loop often, but the function should get called no more than once in 5 min? Commented Aug 6, 2014 at 19:46

3 Answers 3

1

Not sure I understood the question. I'll assume that you want this function to be executed only once every five minutes, no matter how often it is really called.

This might be overkill, but why not use a decorator? This will create a new function for the 'decorated' function that will execute the original function if X seconds have passed since the last execution. This will make sure the function is not executed more than once every 5 minutes (or whateer time interval in seconds you pass to the decorator), no matter whether it's called in that loop or elsewhere.

import time

def onceEveryXSeconds(seconds):             # this creates the decorator
    def wrapper(f):                         # decorator for given 'seconds'
        f.last_execution = 0                # memorize last execution time
        def decorated(*args, **kwargs):     # the 'decorated' function
            if f.last_execution < time.time() - seconds:
                f.last_execution = time.time()
                return f(*args, **kwargs)
        return decorated
    return wrapper

Usage:

@onceEveryXSeconds(3)
def function(foo):
    print foo

while True:
    print "loop"
    function("Hello again")
    time.sleep(1)

Output, with @onceEveryXSeconds(3)

loop
Hello again
loop
loop
loop
Hello again
loop
...
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming the loop takes longer than five minutes, you could use time.time() to determine when 5 minutes has been up.

import time

matches = []
done = []
starttime = time.time()
for item in matches:
    dofunctioneveryloop()
    done.extent(item)
    if time.time() - starttime  > 300:  
        dofunctiononce5min()
        starttime = time.time()

2 Comments

this will do it just once isn't?
Once it reaches inside the conditional, the time that the conditional is comparing to gets reset to the current time.
0

It is not recommended that you do this way. Perhaps the best approach could be to schedule it on operation system, and it run it task periodically.

Anyway, if want to run a statement every x time, here is an example

import time

for i in range(5):
    print i
    time.sleep(3) # seconds

Time as parameter should be fractioned like 0.5 seconds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.