While loops and duplicate code in python -
i have tiny section in code check active threads non-daemon , non-main. these threads needs closed eventually, part check them duplicated follows:
threads = [th th in threading.enumerate() if th not threading.main_thread() or not th.isdaemon()] while threads: threads = [th th in threading.enumerate() if th not threading.main_thread() or not th.isdaemon()] time.sleep(5) exit()
i can try creating variable named count
, check same function threading.active_count()
. try avoid making count
variable whenever can. indeed better code duplication. there other, more elegant, ways this?
threads = [th th in threading.enumerate() if th not threading.main_thread() or not th.isdaemon()] while threads: # pseudocode: kill threads.pop() time.sleep(5) exit()
Comments
Post a Comment