Completed
Push — master ( dfc669...747f62 )
by Raphael
07:12
created

test.ParallelExecutor   A

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  
A __init__() 0 5 1
A target() 0 5 1
B run() 0 26 5
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 run_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
    if executor.stdout_output:
56
        logging.info(executor.stdout_output)
57
    if executor.stderr_output:
58
        logging.info(executor.stderr_output)
59
    if ERROR_KEYWORD in executor.stderr_output:
60
        logging.info("------ Error was found ------")
61
        logging.info(executor.stderr_output)
62
        logging.info("-----------------------------")
63
    else:
64
        logging.info("------ No error was found ------")
65
    return executor.stderr_output