Created
June 9, 2017 00:19
-
-
Save kumikoda/674c9259a4aa2859415491dc703ec8d0 to your computer and use it in GitHub Desktop.
set timeout in python
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
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