Python:时间输入为一分钟并与系统时间进行比较

时间:2014-09-15 05:25:54

标签: python-2.7

我是python的新手。我想根据给定的输入时间(分钟)进行一些操作。

例如,如果我给100分钟..它必须执行下一个100分钟的操作,延迟10秒..

有人可以帮我实现吗?

-Thanks Thamizh

2 个答案:

答案 0 :(得分:0)

  

必须以10秒的延迟执行下一个100分钟的操作。

#!/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()

答案 1 :(得分:0)

这种方法并不比使用Tkinter或Twisted更好,但python初学者可能更容易使用:

#!/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)