ilyaw77
ilyaw77

Reputation: 1047

how to run a method from time a to time b every t minutes

Since I'm new to Python, I need some advice from experienced people. What is the best way to run a Python method from time A to time B every T minutes using only core Python libraries?

To be more specific:

I need single threaded app which will start monitor timestamps of pair of files to make sure that the difference in file creation is always greater than 0. I need to run this monitor only from 9 to 6 every 2 minutes. I will take a look at schedule and time library...

Upvotes: 0

Views: 213

Answers (4)

b10hazard
b10hazard

Reputation: 7809

Is this what you are after?

import time
from datetime import datetime

def doSomething(t,a,b):
    while True:
        if a > b:
            print 'The end date is less than the start date.  Exiting.'
            break
        elif datetime.now() < a:
            # Date format: %Y-%m-%d %H:%M:%S
            now = datetime.now()
            wait_time = time.mktime(time.strptime(str(a),"%Y-%m-%d %H:%M:%S"))-\
                        time.mktime(time.strptime(str(now), "%Y-%m-%d %H:%M:%S.%f"))
            print 'The start date is',wait_time,'seconds from now.  Waiting'
            time.sleep(wait_time)
        elif datetime.now() > b:
            print 'The end date has passed.  Exiting.'
            break
        else:
            # do something, in this example I am printing the local time
            print time.localtime()
            seconds = t*60  # convert minutes to seconds
            time.sleep(seconds) # wait this number of seconds

# date time format is year, month, day, hour, minute, and second
start_date = datetime(2012, 10, 10, 14, 38, 00)
end_date = datetime(2012, 10, 10, 14, 39, 00)
# do something every 2 minutes from the start to end dates
doSomething(2,start_date,end_date)

It will wait until the start date and run the function until the end date. There probably could be some additional error checking depending on what you are doing. Right now all it does is check for invalid entries such as a start date that is greater than an end date. All you have to do is specify the date and times. Hope this helps.

Edit: Ah, I see you updated your question with additional requirements. This method probably won't work for you then.

Upvotes: 0

jfs
jfs

Reputation: 414855

You could:

  1. Use cron (on *nix) or Windows task scheduler to run your script at a desired time.

    It will make your solution both simpler and more robust.

    Or

  2. Run your script as a daemon and subscribe to file system events to monitor your files.

    You could use pyinotify and the like depending on your OS. It provides the best reaction to changes time

Solutions based on time, threading, sched modules are more complex, harder to implement and less reliable.

Upvotes: 1

arilou
arilou

Reputation: 475

import time

#... initislize  A, B and T here

time.sllep(max(0, A - time.time()) # wait for the A moment

while time.time() < B:
    call_your_method()
    time.sleep(T)

Upvotes: 0

Ben
Ben

Reputation: 7124

At first thought something like this might work for you:

import time

# run every T minutes
T = 1
# run process for t seconds
t = 1.

while True:
    start = time.time()

    while time.time() < (start + t):
        print 'hello world'

    print 'sleeping'
    # convert minutes to seconds and subtract the about of time the process ran
    time.sleep(T*60-t)

But there might be a better way, knowing exactly what you're trying to accomplish

Upvotes: 0

Related Questions