Skip to content

Instantly share code, notes, and snippets.

@kubark42
Created August 5, 2020 22:53
Show Gist options
  • Save kubark42/4e6cdb13aa7563aaed769a77b176fe7b to your computer and use it in GitHub Desktop.
Save kubark42/4e6cdb13aa7563aaed769a77b176fe7b to your computer and use it in GitHub Desktop.
import threading
import pause
import random
import time
def doit1(stop_event, name, timeStep):
now = time.time()
print("[" + name + "]: " + str(now))
nextWakeTime = now + timeStep
count = 0
while not stop_event.is_set():
print ("working on %s\n" % name)
pause.until(nextWakeTime)
nextWakeTime = nextWakeTime + timeStep
count = count + 1
print("Stopping %s.\n" % name)
print("total time delta: " + str(nextWakeTime-now - timeStep))
print("Avg timestep: " + str((nextWakeTime-now - timeStep) / (count)))
print("Requested timestep: " + str(timeStep))
print("Accuracy: " + str(((nextWakeTime-now - timeStep) / (count))/timeStep * 100))
pill2kill = threading.Event()
t1 = threading.Thread(target=doit1, args=(pill2kill, "bob", 0.5))
t2 = threading.Thread(target=doit1, args=(pill2kill, "fred", 1.0))
t1.start()
t2.start()
time.sleep(2.9)
pill2kill.set()
t2.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment