Skip to content

Instantly share code, notes, and snippets.

@kumikoda
Created June 9, 2017 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumikoda/674c9259a4aa2859415491dc703ec8d0 to your computer and use it in GitHub Desktop.
Save kumikoda/674c9259a4aa2859415491dc703ec8d0 to your computer and use it in GitHub Desktop.
set timeout in python
import heapq
import time
class runtime:
def __init__(self):
self.heap = []
def setTimeout(self, fn, timeout):
exp = time.time() + timeout
heapq.heappush(self.heap, (exp, fn))
def run(self):
while len(self.heap) > 0:
print time.time()
if time.time() > self.heap[0][0]:
exp, fn = heapq.heappop(self.heap)
fn()
time.sleep(1)
def _10():
print 10
r.setTimeout(_3, 3)
def _3():
print 3
pass
r = runtime()
r.setTimeout(_10, 10)
r.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment