Skip to content

Instantly share code, notes, and snippets.

@alanduan
Created October 13, 2015 00:28
Show Gist options
  • Save alanduan/beee29f45d20737644ae to your computer and use it in GitHub Desktop.
Save alanduan/beee29f45d20737644ae to your computer and use it in GitHub Desktop.
heartbeat demo
import threading
def foo():
alive = True
c = threading.Condition()
def heartbeater():
heartbeat_interval = 60
heartbeat_threshold = 10
c.acquire()
while alive:
send_heartbeat(heartbeat_interval * heartbeat_threshold)
c.wait(heartbeat_interval)
c.release()
t = threading.Thread(target=heartbeater)
t.deamon = True
t.start()
for i in range(100):
# do the real job
os.sleep(10)
c.acquire()
c.notify()
c.release()
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment