3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
14 from future.utils
import viewitems
18 Sometimes CUDA devices can get stuck, 'deadlock'. In this case it is often 19 better just the kill the process automatically. Use this guard to set a 20 maximum timespan for a python call, such as RunNet(). If it does not complete 21 in time, process is killed. 24 with timeout_guard.CompleteInTimeOrDie(10.0): 31 def __init__(self, timeout_secs):
32 threading.Thread.__init__(self)
41 self.condition.acquire()
43 self.condition.wait(self.
timeout_secs - (time.time() - started))
44 self.condition.release()
46 log = logging.getLogger(
"timeout_guard")
47 log.error(
"Call did not finish in time. Timeout:{}s PID: {}".format(
55 log.info(
"Prepared output, dumping threads. ")
57 print(
"-----After force------")
61 for threadId, stack
in viewitems(sys._current_frames()):
62 if threadId == self.caller_thread.ident:
63 code.append(
"\n# ThreadID: %s" % threadId)
64 for filename, lineno, name, line
in traceback.extract_stack(stack):
65 code.append(
'File: "%s", line %d, in %s' % (filename, lineno, name))
67 code.append(
" %s" % (line.strip()))
69 print(
"\n".join(code))
70 log.error(
"Process did not terminate cleanly in 10 s, forcing")
73 forcet = threading.Thread(target=forcequit, args=())
77 print(
"-----Before forcing------")
81 for threadId, stack
in viewitems(sys._current_frames()):
82 code.append(
"\n# ThreadID: %s" % threadId)
83 for filename, lineno, name, line
in traceback.extract_stack(stack):
84 code.append(
'File: "%s", line %d, in %s' % (filename, lineno, name))
86 code.append(
" %s" % (line.strip()))
88 print(
"\n".join(code))
89 os.kill(os.getpid(), signal.SIGINT)
92 @contextlib.contextmanager
93 def CompleteInTimeOrDie(timeout_secs):
97 watcher.completed =
True 98 watcher.condition.acquire()
99 watcher.condition.notify()
100 watcher.condition.release()
103 def EuthanizeIfNecessary(timeout_secs=120):
105 Call this if you have problem with process getting stuck at shutdown. 106 It will kill the process if it does not terminate in timeout_secs.