0

I am new to python.. I want to do some operation based on the given input time(minute).

For Example if i give 100min.. It has to execute the operation for next 100min with the delay of 10sec..

could somebody help me implement it?

-Thanks Thamizh

Thamizh
  • 1
  • 1

2 Answers2

0

It has to execute the operation for next 100min with the delay of 10sec..

#!/usr/bin/env python
from Tkinter import Tk

def repeat(delay, operation, *args):
    """Call operation(*args) repeatedly with the delay after each call."""
    if not stopping:
        # execute operation
        operation(*args)
        # repeat the call
        root.after(delay*1000, repeat, delay, operation, *args)
    else:
        root.destroy() # exit mainloop

stopping = []
root = Tk()
root.withdraw() # don't show the GUI window
root.after(100*60*1000, stopping.append, True) # stop in 100 minutes
root.after(10000, repeat, 10, operation, arg1) # call repeat() in 10 seconds
root.mainloop()
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • That might be a little extreme if all he really wanted was to use a loop and a delay (like time.sleep()). Plus, he didn't define whether he wanted a metronomic iteration or a strict 10 second gap (in case some operations take longer). – Alex North-Keys Sep 15 '14 at 06:04
  • @AlexNorth-Keys: see [`call_every()` function in my answer](http://stackoverflow.com/a/14040516/4279) if you need a metronomic iteration. You are welcome to provide a simpler less extendible answer e.g., what do you do if you need to run 2 functions with different delays that may work on shared data? – jfs Sep 15 '14 at 06:30
  • My point was that *he* might not need to go as far as the Tkinter approach (aside: "call_every" isn't in the current version of your answer). If he's a beginner, your example could be confusing in a number of ways, not the least of which is repeat's use of a bunch of globals (root, stopping). For someone more experienced, it could seem odd to drag in a widget toolkit just to do timing. – Alex North-Keys Sep 15 '14 at 16:33
0

This approach isn't better than using Tkinter or Twisted, but it might be easier for a python beginner to work with:

#!/usr/bin/python

import argparse
import time

def myoperation():     # you define this to be whatever you need python to do
    print('myop')      # ... instead of just printing "myop"

def perform(duration, period, function):
    '''
    Controls how often your function gets executed.
    duration - overall time to perform the function, in seconds.
    period   - fire off the function every period seconds.
    '''
    end = time.time() + duration
    while True:
        before = time.time()
        if before > end:
            break
        function()
        after = time.time()
        time.sleep(period - (after - before))

# Provide parsing for a command line like: ./<thisprog> <testlength> <interval>
# To see help, run: ./<thisprog> --help
# (use the actual file name instead of <thisprog>
# To make the script runnable this way, run: chmod +x <thisprog>
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Perform a test.')
    parser.add_argument('duration', type=int, default=60,
                        help=('Seconds to repeat test, default 60'))
    parser.add_argument('period', type=int, default=10,
                        help=('Time between test starts, default 10s'))
    args = parser.parse_args()
    print('args: %r' % (args,))
    perform(args.duration, args.period, myoperation)
Alex North-Keys
  • 4,200
  • 1
  • 20
  • 22
  • I agree with you the approach based on the event-loop (that is why tkinter is used) provides more than OP needs. Thank you for posting the simpler version. btw, there is nothing wrong with the globals here -- Python doesn't force you to create classes. – jfs Sep 15 '14 at 16:56
  • beware, `time.time()` can be set back (there is `time.monotonic()`). `time.sleep()` may return earlier/later -- it might be too simplistic to implement "metronomic" version. `function()` can take longer than `period`. – jfs Sep 15 '14 at 17:14
  • I wasn't aware time.time() could return earlier results other than if an admin changed the system clock (on unix at least, and I'm honestly not too worried about that case). It could indeed skip an invocation - although I think adding a warning on output to that effect when it happens would be less confusing than running overlapping test cases. I don't have a time.monotonic() on my system. The accuracy issues of time.sleep() are worth worrying about in some contexts, it's true - but *fully* dealing with the issue (swapped out, for example) is complex. – Alex North-Keys Sep 15 '14 at 20:26
  • ntp can cause the time to jump, you don't need an admin. `time.monotonic()` is available on all systems supported by CPython. It is introduced in Python 3.3. `time.sleep()` can be interrupted (whatever the accuracy) *and* the accuracy might not be enough if there are many periods to provide "metronomic" iterations. – jfs Sep 16 '14 at 01:32
  • The python 3.3 with time.monotonic() makes sense - a lot of us are still trapped in 2.7 or so from various things. Your other comments on sleep are all true, and agree with my comments before. – Alex North-Keys Sep 16 '14 at 05:23
  • 1
    unrelated: you can get Python 3.3 `time` functions in Python 2. [Here's an implementation for Ubuntu (it might work on other POSIX systems)](https://gist.github.com/zed/5073409) – jfs Sep 16 '14 at 19:40