Skip to content

Instantly share code, notes, and snippets.

@mrahtz
Created Mar 9, 2019
Embed
What would you like to do?
#!/usr/bin/env python3
import argparse
import os
import subprocess
import socket
import tempfile
import getpass
parser = argparse.ArgumentParser()
parser.add_argument('run_dirs', nargs='*')
args = parser.parse_args()
def get_open_port():
s = socket.socket()
s.bind(('', 0))
port = s.getsockname()[1]
s.close()
return port
port = get_open_port()
if len(args.run_dirs) == 1:
tensorboard_dir = args.run_dirs[0]
else:
tensorboard_dir = tempfile.mkdtemp()
for path in args.run_dirs:
link = os.path.join(tensorboard_dir, path)
os.makedirs(os.path.dirname(link), exist_ok=True)
os.symlink(os.path.abspath(path), link)
subprocess.run(['pkill', '-f', 'tensorboard --port {}'.format(port)])
proc1 = subprocess.Popen(['tensorboard', '--port', str(port), '--logdir', tensorboard_dir],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("Waiting for TensorBoard to start...")
proc1.stderr.readline()
subprocess.run(['pkill', '-f', 'ngrok'])
proc2 = subprocess.Popen(['n', str(port)])
try:
input()
except KeyboardInterrupt:
pass
proc1.terminate()
proc2.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment