Started basic rescue script
This commit is contained in:
parent
843d282dfc
commit
9842de8859
9 changed files with 260 additions and 8 deletions
39
openflexure_microscope/rescue/monitor_timeout.py
Normal file
39
openflexure_microscope/rescue/monitor_timeout.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import multiprocessing
|
||||
import logging
|
||||
import time
|
||||
|
||||
# bar
|
||||
def test_long_fn():
|
||||
for _ in range(100):
|
||||
print("Tick")
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def launch_timeout_test_process(target, args=(), kwargs=None, timeout=10):
|
||||
if not kwargs:
|
||||
kwargs = {}
|
||||
|
||||
# Start target as a process
|
||||
p = multiprocessing.Process(target=target, args=args, kwargs=kwargs)
|
||||
p.start()
|
||||
|
||||
# Wait for 10 seconds or until process finishes
|
||||
p.join(timeout)
|
||||
|
||||
# If thread is still active
|
||||
if p.is_alive():
|
||||
logging.error(
|
||||
f"Function {target} reached timeout after {timeout} seconds. Terminating."
|
||||
)
|
||||
|
||||
# Terminate
|
||||
p.terminate()
|
||||
p.join()
|
||||
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
launch_timeout_test_process(test_long_fn, timeout=5)
|
||||
Loading…
Add table
Add a link
Reference in a new issue