bin.ParallelExecutor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %
Metric Value
dl 0
loc 33
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 26 5
A target() 0 5 1
A __init__() 0 5 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import os, sys
5
import subprocess, threading
6
import logging as loggers
7
import time
8
loggers.basicConfig(level=loggers.INFO)
9
logging = loggers.getLogger(__name__)
10
11
12
class ParallelExecutor(object):
13
    def __init__(self, cmd):
14
        self.cmd = cmd
15
        self.process = None
16
        self.stdout_output = ""
17
        self.stderr_output = ""
18
19
    def run(self, timeout):
20
        def target():
21
            logging.info(self.cmd)
22
            self.process = subprocess.Popen(self.cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
23
            self.stdout_output, self.stderr_output = self.process.communicate()
24
            logging.info("Thread finished")
25
26
        thread = threading.Thread(target=target)
27
        thread.daemon = True
28
        thread.start()
29
30
        thread.join(timeout)
31
        if thread.is_alive():
32
            logging.info("Time out, terminating process")
33
            self.process.terminate()
34
            time.sleep(1)
35
            try:
36
                self.process.kill()
37
            except OSError:
38
                pass
39
            # thread.join()
40
        try:
41
            thread._stop()
42
        except:
43
            pass
44
        return self.process.returncode
45
46
ERROR_KEYWORD = "Traceback (most recent call last)"
47
48
def test_experiment(path, timeout=60):
49
    path = os.path.join("experiments", path)
50
    executor = ParallelExecutor("python %s" % path)
51
    executor.run(timeout=timeout)
52
    if executor.stderr_output == None:
53
        logging.info("stderr is none")
54
        return
55
    logging.info(executor.stdout_output)
56
    logging.info(executor.stderr_output)
57
    if ERROR_KEYWORD in executor.stderr_output:
58
        logging.info("------ Error was found ------")
59
        logging.info(executor.stderr_output)
60
        logging.info("-----------------------------")
61
    else:
62
        logging.info("------ No error was found ------")
63
    assert ERROR_KEYWORD not in executor.stderr_output
64
65
66
if __name__ == '__main__':
67
    # MNIST
68
    test_experiment("mnist/mlp_dropout.py", timeout=30)
69
    test_experiment("mnist/deep_convolution.py", timeout=30)
70
    # Auto-encoders
71
    test_experiment("auto_encoders/rnn_auto_encoder.py", timeout=120)
72
    # LMs
73
    # test_experiment("lm/baseline_rnnlm.py", timeout=180)
74
    # Highway networks
75
    test_experiment("highway_networks/mnist_highway.py", timeout=120)
76
    # CG Training
77
    test_experiment("scipy_training/mnist_cg.py", timeout=180)
78
    # Tutorials
79
    test_experiment("tutorials/tutorial1.py", timeout=120)
80
    test_experiment("tutorials/tutorial2.py", timeout=120)
81
    test_experiment("tutorials/tutorial3.py", timeout=120)
82
    sys.exit(0)
83