Skip to content

Instantly share code, notes, and snippets.

@Zantier
Last active February 6, 2020 15:59
Show Gist options
  • Save Zantier/ecdf67ac20bc2d3444b5ef084db33420 to your computer and use it in GitHub Desktop.
Save Zantier/ecdf67ac20bc2d3444b5ef084db33420 to your computer and use it in GitHub Desktop.
python: Create thread by function or class
from threading import Thread
import time
class Timer(Thread):
def __init__(self, func, time):
super().__init__()
self.func = func
self.time = time
def run(self):
time.sleep(self.time)
self.func()
def testFunc():
print("Time up!")
def runInThread(func):
thread = Thread(target=testFunc)
thread.start()
def setTimeout(func, time):
timer = Timer(func, time)
timer.start()
if __name__ == '__main__':
runInThread(testFunc)
setTimeout(testFunc, 1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment