-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathfunc.py
65 lines (57 loc) · 1.96 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import subprocess, threading
import logging as loggers
import time
loggers.basicConfig(level=loggers.INFO)
logging = loggers.getLogger(__name__)
class ParallelExecutor(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
self.stdout_output = ""
self.stderr_output = ""
def run(self, timeout):
def target():
logging.info(self.cmd)
self.process = subprocess.Popen(self.cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.stdout_output, self.stderr_output = self.process.communicate()
logging.info("Thread finished")
thread = threading.Thread(target=target)
thread.daemon = True
thread.start()
thread.join(timeout)
if thread.is_alive():
logging.info("Time out, terminating process")
self.process.terminate()
time.sleep(1)
try:
self.process.kill()
except OSError:
pass
# thread.join()
try:
thread._stop()
except:
pass
return self.process.returncode
ERROR_KEYWORD = "Traceback (most recent call last)"
def run_experiment(path, timeout=60):
path = os.path.join("experiments", path)
executor = ParallelExecutor("python %s" % path)
executor.run(timeout=timeout)
if executor.stderr_output == None:
logging.info("stderr is none")
return
if executor.stdout_output:
logging.info(executor.stdout_output)
if executor.stderr_output:
logging.info(executor.stderr_output)
if ERROR_KEYWORD in executor.stderr_output:
logging.info("------ Error was found ------")
logging.info(executor.stderr_output)
logging.info("-----------------------------")
else:
logging.info("------ No error was found ------")
return executor.stderr_output