Skip to content

Instantly share code, notes, and snippets.

@rmehta
Last active May 20, 2025 17:33
Show Gist options
  • Save rmehta/395735f5257b6771d47886a16e753660 to your computer and use it in GitHub Desktop.
Save rmehta/395735f5257b6771d47886a16e753660 to your computer and use it in GitHub Desktop.
import threading
import time
working_flag = False
pending_flag = False
def run_test():
global working_flag, pending_flag
if working_flag:
# request triggered when the job is running, so set pending
pending_flag = True
print("bounced")
return
# run task in background
threading.Thread(target = test).start()
def test():
global working_flag, pending_flag
working_flag = True
print("starting")
time.sleep(2)
print("done")
working_flag = False
if pending_flag:
# we got another request, so run once more
pending_flag = False
run_test()
if __name__ == "__main__":
run_test()
run_test()
run_test()
run_test()
time.sleep(2)
run_test()
run_test()
run_test()
# output
"""
> python3 test_thread.py
starting
bounced
bounced
bounced
bounced
bounced
bounced
done
starting
done
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment