Last active
February 6, 2020 15:59
-
-
Save Zantier/ecdf67ac20bc2d3444b5ef084db33420 to your computer and use it in GitHub Desktop.
python: Create thread by function or class
This file contains hidden or 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(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