Created
October 13, 2015 00:28
-
-
Save alanduan/beee29f45d20737644ae to your computer and use it in GitHub Desktop.
heartbeat demo
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 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