Skip to content

Instantly share code, notes, and snippets.

@dsociative
Created February 16, 2013 03:03
Show Gist options
  • Save dsociative/4965325 to your computer and use it in GitHub Desktop.
Save dsociative/4965325 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import time
import threading
from itertools import repeat
class KeepAliveThread(threading.Thread):
nodes = []
@classmethod
def register(cls, node):
cls.nodes.append(node)
def __init__(self, timeout=6):
super(KeepAliveThread, self).__init__()
self.stoprequest = threading.Event()
self.timeout = timeout
def join(self, timeout=None):
self.stoprequest.set()
super(KeepAliveThread, self).join(timeout)
def sleep_or_not(self, sec):
if not self.stoprequest.isSet():
time.sleep(sec)
def wait(self):
map(self.sleep_or_not, repeat(1, self.timeout))
def run(self):
while not self.stoprequest.isSet():
for node in self.nodes:
print node
self.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment