Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python version of settimout

from datetime import datetime, timedelta
import heapq

# just holds a function, its arguments, and when we want it to execute.
class TimeoutFunction:
    def __init__(self, function, timeout, *args):
        self.function = function
        self.args = args
        self.startTime = datetime.now() + timedelta(0,0,0,timeout) 

    def execute(self):
        self.function(*self.args)

# A "todo" list for all the TimeoutFunctions we want to execute in the future
# They are sorted in the order they should be executed, thanks to heapq
class TodoList: 
    def __init__(self):
        self.todo = []

    def addToList(self, tFunction):
        heapq.heappush(self.todo, (tFunction.startTime, tFunction))

    def executeReadyFunctions(self):
        if len(self.todo) > 0:
            tFunction = heapq.heappop(self.todo)[1]
            while tFunction and datetime.now() > tFunction.startTime:
                #execute all the functions that are ready
                tFunction.execute()
                if len(self.todo) > 0:
                    tFunction = heapq.heappop(self.todo)[1]
                else:
                    tFunction = None                    
            if tFunction:
                #this one's not ready yet, push it back on
                heapq.heappush(self.todo, (tFunction.startTime, tFunction))

def singleArgFunction(x):
    print str(x)

def multiArgFunction(x, y):
    #Demonstration of passing multiple-argument functions
    print str(x*y)

# Make some TimeoutFunction objects
# timeout is in milliseconds
a = TimeoutFunction(singleArgFunction, 1000, 20)
b = TimeoutFunction(multiArgFunction, 2000, *(11,12))
c = TimeoutFunction(quit, 3000, None)

todoList = TodoList()
todoList.addToList(a)
todoList.addToList(b)
todoList.addToList(c)

while True:
    todoList.executeReadyFunctions()
Comment

PREVIOUS NEXT
Code Example
Python :: replace nan in pandas column with mode and printing it 
Python :: how to import data in python 
Python :: space complexity python 
Python :: python types 
Python :: Example of break, continue and pass statements in python 
Python :: python list comprehensions 
Python :: print() function in python 
Python :: how to do more than or less than as a condition in pythonb 
Python :: full form of api 
Python :: python how to restart thread 
Python :: extend python 
Python :: clear 
Python :: python remove character from string 
Python :: python pandas how to access a column 
Python :: how to print second largest number in python 
Python :: python create empty list size n 
Python :: login required 
Python :: python keyword arguments 
Python :: how to use djoser signals 
Python :: install python anaconda 
Python :: write hexadecimal in python 
Python :: python copy vs deepcopy 
Python :: print variable python 
Python :: How to swap elements in a list in Python detailed 
Python :: second highest value in list python 
Python :: generating random numbers numpy 
Python :: python lenght 
Python :: # keys in python 
Python :: simulation? 
Python :: heroku procfile 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =