Skip to content

Instantly share code, notes, and snippets.

@jaeoh2
Created April 13, 2021 14:13
Show Gist options
  • Save jaeoh2/d5a5ccd07e8ff57e5224099db969dc61 to your computer and use it in GitHub Desktop.
Save jaeoh2/d5a5ccd07e8ff57e5224099db969dc61 to your computer and use it in GitHub Desktop.
Python threading Timer callback example
import time
import threading
ser = 0
start_time = time.time()
def callback():
global ser, start_time
if ser == 1:
print("Open {}, {}".format(time.time() - start_time, ser))
ser = 2
elif ser == 2:
print("thread started {}, {}".format(time.time() - start_time, ser))
else:
print("OS Error {}, {}".format(time.time(), ser))
time.sleep(1)
ser = 1
threading.Timer(0.1, callback).start() # run callback in every 0.1 sec
start_time = time.time()
if __name__=="__main__":
callback()
while True:
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment