MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / PythonScript

Class PythonScript

tests/case_script.py:11–57  ·  view source on GitHub ↗

A wrapper to start a python script with timeout. To test the actual models even without GPUs we simply start them and test whether they survive a certain amount of time "timeout". This allows to test if all imports are correct and the computation graph can be built without run the e

Source from the content-addressed store, hash-verified

9
10
11class PythonScript(threading.Thread):
12 """A wrapper to start a python script with timeout.
13
14 To test the actual models even without GPUs we simply start them and
15 test whether they survive a certain amount of time "timeout". This allows to
16 test if all imports are correct and the computation graph can be built without
17 run the entire model on the CPU.
18
19 Attributes:
20 cmd (str): command to execute the example with all flags (including python)
21 p: process handle
22 timeout (int): timeout in seconds
23 """
24 def __init__(self, cmd, timeout):
25 """Prepare a python script
26
27 Args:
28 cmd (str): command to execute the example with all flags (including python)
29 timeout (int): time in seconds the script has to survive
30 """
31 threading.Thread.__init__(self)
32 self.cmd = cmd
33 self.timeout = timeout
34
35 def run(self):
36 self.p = subprocess.Popen(shlex.split(self.cmd), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
37 self.out, self.err = self.p.communicate()
38
39 def execute(self):
40 """Execute python script in other process.
41
42 Raises:
43 SurviveException: contains the error message of the script if it terminated before timeout
44 """
45 self.start()
46 self.join(self.timeout)
47
48 if self.is_alive():
49 self.p.terminate()
50 self.p.kill() # kill -9
51 self.join()
52 else:
53 # something unexpected happend here, this script was supposed to survive at least the timeout
54 if len(self.err) > 0:
55 output = u"STDOUT: \n\n\n" + self.out.decode('utf-8')
56 output += u"\n\n\n STDERR: \n\n\n" + self.err.decode('utf-8')
57 raise AssertionError(output)
58
59
60class TestPythonScript(unittest.TestCase):

Callers 1

assertSurviveMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected