Created
March 2, 2017 22:04
-
-
Save CatherineH/bbfedca39ec7261e356df108c599d33f to your computer and use it in GitHub Desktop.
an example of a python script that watches all threads for early termination
This file contains 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, enumerate | |
import logging | |
def fails(): | |
for i in range(999999999): | |
assert i < 9999999 | |
def succeeds(): | |
while True: | |
assert 2 == 2 | |
def watcher(): | |
init_threads = enumerate() | |
while True: | |
curr_threads = enumerate() | |
if len(curr_threads) < len(init_threads): | |
for thread in init_threads: | |
if thread not in curr_threads: | |
logging.error(thread.name + " not found") | |
init_threads = curr_threads | |
if __name__ == "__main__": | |
t1 = Thread(target=succeeds, name="succeeds") | |
t2 = Thread(target=fails, name="fails") | |
t3 = Thread(target=watcher, name="watcher") | |
t1.start() | |
t2.start() | |
t3.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment