Skip to content

Instantly share code, notes, and snippets.

@JCGrant
Created May 30, 2017 19:46
Show Gist options
  • Save JCGrant/b1d8ff4af747f9eb2808414b2fb591c7 to your computer and use it in GitHub Desktop.
Save JCGrant/b1d8ff4af747f9eb2808414b2fb591c7 to your computer and use it in GitHub Desktop.
Running a timer object in a separate thread in Python
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