Skip to content

Instantly share code, notes, and snippets.

@cetanu
Created February 16, 2022 15:43
Show Gist options
  • Save cetanu/2dda87271cad9fa3a3cb1f8896b413d7 to your computer and use it in GitHub Desktop.
Save cetanu/2dda87271cad9fa3a3cb1f8896b413d7 to your computer and use it in GitHub Desktop.
fun with threads
import time
import threading
def thing(key, value, queue, results):
print(f"Processing {key=} {value=}")
# what happens to the thread when there's an exception here?
# the thread should catch it and remove the job from the queue so that
# another can be enqueued.
results[key] = value ** 2
del queue[key]
def main():
jobs = {}
complete = {}
tasks = {f"count{n}": n for n in [5, 10, 20, 30, 40, 60]}
try:
while True:
print("------------------------------")
print(f"CURRENT QUEUE: {len(jobs)}")
print(f"Jobs: {jobs}")
print(f"Results: {complete}")
for key, value in tasks.items():
try:
if jobs[key].is_alive():
# job still executing from last time
# do some kind of timeout?
# Maybe pass a start time to the thread, and compare
# with interval/refresh/end date.
print(f"JOB {key} ALREADY RUNNING")
continue
except KeyError:
pass
print(f"JOB {key} QUEUED")
# This has to be run one time, before the while loop, with no timer
# to prepopulate the data on boot
jobs[key] = threading.Timer(
function=thing,
args=(key, value, jobs, complete),
interval=value,
)
jobs[key].daemon = True
jobs[key].start()
print("------------------------------")
time.sleep(10)
except KeyboardInterrupt:
print("Gracefully exiting")
exit(0)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment