Commit 509dcb40 by Grinaway

added watchdog class from stackoverflow

parent cbe0efa1
...@@ -7,6 +7,32 @@ import os ...@@ -7,6 +7,32 @@ import os
import sys import sys
import numpy import numpy
from threading import Timer
#from a solution on stackoverflow
class Watchdog:
def __init__(self, timeout, userHandler=None): # timeout in seconds
self.timeout = timeout
self.handler = userHandler if userHandler is not None else self.defaultHandler
self.timer = Timer(self.timeout, self.handler)
def reset(self):
self.timer.cancel()
self.timer = Timer(self.timeout, self.handler)
def stop(self):
self.timer.cancel()
def defaultHandler(self):
raise self
def simulate(pdbcode, pdb_filename): def simulate(pdbcode, pdb_filename):
from simtk.openmm import app from simtk.openmm import app
import simtk.openmm as mm import simtk.openmm as mm
...@@ -98,9 +124,10 @@ def test_build_and_simulate(): ...@@ -98,9 +124,10 @@ def test_build_and_simulate():
infile = open(input_pdb_filename) infile = open(input_pdb_filename)
outfile = open(output_pdb_filename, 'w') outfile = open(output_pdb_filename, 'w')
from eventlet.timeout import Timeout
timeout_seconds = 30.0 timeout_seconds = 30.0
with Timeout(timeout_seconds) as timeout: watchdog = Watchdog(timeout_seconds)
try:
from pdbfixer.pdbfixer import PDBFixer, PdbStructure from pdbfixer.pdbfixer import PDBFixer, PdbStructure
from simtk.openmm import app from simtk.openmm import app
stage = "Creating PDBFixer..." stage = "Creating PDBFixer..."
...@@ -124,24 +151,20 @@ def test_build_and_simulate(): ...@@ -124,24 +151,20 @@ def test_build_and_simulate():
stage = "Done." stage = "Done."
infile.close() infile.close()
outfile.close() outfile.close()
except Watchdog:
print "timed out fixing PDB %s" % pdbcode
# Test simulating this with OpenMM. # Test simulating this with OpenMM.
watchdog.stop()
del watchdog
if pdbcode in pdbcodes_to_simulate: if pdbcode in pdbcodes_to_simulate:
timeout_seconds = 30.0 watchdog = Watchdog(timeout_seconds)
timeout = Timeout(timeout_seconds)
try: try:
simulate(pdbcode, output_pdb_filename) simulate(pdbcode, output_pdb_filename)
except Timeout as e: except Watchdog:
print str(e)
timeout.cancel()
print "PDB code %s timed out in stage '%s'." % (pdbcode, stage) print "PDB code %s timed out in stage '%s'." % (pdbcode, stage)
except Exception as e:
print str(e)
timeout.cancel()
success = False success = False
watchdog.stop()
# Clean up. # Clean up.
os.remove(input_pdb_filename) os.remove(input_pdb_filename)
os.remove(output_pdb_filename) os.remove(output_pdb_filename)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment