Skip to content

Commit

Permalink
fix runner to work with debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
crboth committed Oct 5, 2016
1 parent 5b2f49c commit 492c6a4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
soccer
log*/

logs/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
92 changes: 54 additions & 38 deletions run_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,90 @@

parser = argparse.ArgumentParser(description="deep soccer evaluator")
parser.add_argument("--glue_port", type=str, default="4096",
help="rlglue port (default 4096)")
args = parser.parse_known_args()[0]
help="rlglue port (default 4096)")
parser.add_argument('--logdir', type=str, default='./logs',
help='dir to keep logs in')
# args = parser.parse_known_args()[0]
args = parser.parse_args()
my_env = os.environ.copy()

my_env["RLGLUE_PORT"] = args.glue_port
my_env['PATH'] = '/usr/local/bin:' + my_env['PATH']

time_dir = os.path.abspath(
datetime.datetime.now().strftime("./log_%Y%m%d(%H:%M:%S)"))
datetime.datetime.now().strftime("{}/log_%Y%m%d(%H:%M:%S)".format(args.logdir))
)

try:
os.makedirs(time_dir)
os.makedirs(time_dir)
except os.error:
print "directory already exists"
sys.exit(0)
print "directory already exists"
sys.exit(0)

logfiles = []


def run(cmd, logdir, logfiles):
global my_env
cmdname = cmd.split(" ")[0]
fname = os.path.splitext(ntpath.basename(cmdname))[0]
cmdname = cmd.split(" ")[0]
fname = os.path.splitext(ntpath.basename(cmdname))[0]

logpath = os.path.join(logdir, fname+"_log")
errpath = os.path.join(logdir, fname+"_err")
logpath = os.path.join(logdir, fname+"_log")
errpath = os.path.join(logdir, fname+"_err")

print "cmd:", cmd
print "logpath:", logpath
print "errpath:", errpath
print
print "cmd:", cmd
print "logpath:", logpath
print "errpath:", errpath
print

logfile = open(logpath, "w")
errfile = open(errpath, "w")
logfiles.append(logfile)
logfiles.append(errfile)
runcmd = ['/usr/bin/env', ] + shlex.split(cmd)
# runcmd = shlex.split(cmd)
p = subprocess.Popen(runcmd, shell=False, universal_newlines=True,
stdout=logfile, stderr=errfile, env=my_env)

logfile = open(logpath, "w")
errfile = open(errpath, "w")
logfiles.append(logfile)
logfiles.append(errfile)
runcmd = shlex.split(cmd)
p = subprocess.Popen(runcmd, shell=False, universal_newlines=True,
stdout=logfile, stderr=errfile, env=my_env)

return p

return p
mypath = os.path.dirname(os.path.realpath(__file__))

p_core = run("rl_glue", time_dir, logfiles)
p_agent = run("./python/agent.py", time_dir, logfiles)
p_experiment = run("./python/experiment.py {}".format(time_dir),
time_dir, logfiles)
p_agent = run(sys.executable + ' ' + os.path.join(mypath, "python/agent.py"), time_dir, logfiles)
p_experiment = run(sys.executable + ' ' + os.path.join(mypath, "python/experiment.py {}".format(time_dir)),
time_dir, logfiles)

p_environment = run("./soccer", time_dir, logfiles)
p_environment = run(os.path.join(mypath, "soccer"), time_dir, logfiles)

def close(process):
if process.poll():
# None if the process is alive
process.terminate()
if process.poll() is None:
# None if the process is alive
print 'killing', process
process.terminate()


@atexit.register
def cleanup():
global logfiles, p_core, p_agent, p_experiment, p_environment
print "program exiting"
[l.flush() for l in logfiles]
[close(p) for p in (p_core, p_agent, p_experiment, p_environment)]
sys.exit(0)
global logfiles, p_core, p_agent, p_experiment, p_environment
print "program exiting"
for l in logfiles:
try:
l.flush()
except:
pass

for p in (p_core, p_agent, p_environment, p_experiment):
try:
close(p)
except:
pass
sys.exit(0)

def signal_handler(signal, frame):
print "caught ctrl-c"
cleanup()
sys.exit(0)
print "caught ctrl-c"
cleanup()
sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

Expand Down

0 comments on commit 492c6a4

Please sign in to comment.