Created
April 10, 2011 00:52
-
-
Save nsisodiya/911942 to your computer and use it in GitHub Desktop.
Python FunctionScheduler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#(c) 2011 , Narendra Sisodiya , narendra@narendrasisodiya.com | |
# Sunday, 10 April 2011 | |
# Released under MIT License | |
# | |
# PythonTimer is a timer class, you can download it from | |
# http://code.activestate.com/recipes/577646-pythontimer/ | |
import threading | |
import PythonTimer | |
import time | |
def hello(): | |
print "hello, world" | |
class FunctionScheduler(): | |
"This will implement the function timers class" | |
def __init__(self, duration, function, args=[], kwargs={}): | |
self.functionName = function | |
self.duration = duration | |
self.args = args | |
self.kwargs = kwargs | |
self.thread = threading.Timer(self.duration, self.functionName, self.args, self.kwargs) | |
self.timer = PythonTimer.TickTockTimer() | |
def Start(self): | |
self.thread.start() | |
self.timer.StartTimer() | |
def Cancel(self): | |
self.thread.cancel() | |
def Pause(self): | |
self.thread.cancel() | |
self.timer.Pause() | |
def UnPause(self): | |
self.duration = self.duration - self.timer.GetTime() | |
self.thread = threading.Timer(self.duration, self.functionName, self.args, self.kwargs) | |
self.thread.start() | |
self.timer.StartTimer() | |
def TimeLeft(self): | |
return self.duration - self.timer.GetTime() | |
myfunc = FunctionScheduler(8.0, hello) | |
myfunc.Start() | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
myfunc.Pause() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
myfunc.UnPause() | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() | |
time.sleep(1) | |
print myfunc.TimeLeft() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment