Created
May 30, 2017 19:46
-
-
Save JCGrant/b1d8ff4af747f9eb2808414b2fb591c7 to your computer and use it in GitHub Desktop.
Running a timer object in a separate thread 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
from threading import Thread | |
import time | |
class Timer(): | |
def __init__(self): | |
self.time = 0 | |
def run(self): | |
while True: | |
self.time += 1 | |
time.sleep(1) | |
def display(self): | |
print(self.time) | |
timer = Timer() | |
timer_thread = Thread(target=timer.run, daemon=True) | |
timer_thread.start() | |
time.sleep(0.5) | |
while True: | |
timer.display() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment