Update examples

This commit is contained in:
allegroai
2020-03-05 19:56:51 +02:00
parent ef73bc258f
commit 8acb236b33
2 changed files with 38 additions and 20 deletions

View File

@@ -4,10 +4,14 @@ import subprocess
from copy import deepcopy
import socket
from tempfile import mkstemp
# make sure we have jupter in the auto requirements
import jupyter
from trains import Task
# set default docker image, with network configuration
os.environ['TRAINS_DOCKER_IMAGE'] = 'nvidia/cuda --network host'
# initialize TRAINS
task = Task.init(project_name='examples', task_name='Remote Jupyter NoteBook')
@@ -21,33 +25,45 @@ for key in os.environ:
if key.startswith('TRAINS') and key not in preserve:
env.pop(key, None)
# Add jupyter server base folder
param = {
'jupyter_server_base_directory': '',
}
task.connect(param)
# execute jupyter notebook
fd, local_filename = mkstemp()
print('Running Jupyter Notebook Server on {} [{}]'.format(socket.gethostname(), socket.gethostbyname(socket.gethostname())))
process = subprocess.Popen([sys.executable, '-m', 'jupyter', 'notebook'], env=env, stdout=fd, stderr=fd)
cwd = os.path.expandvars(os.path.expanduser(param['jupyter_server_base_directory'])) \
if param['jupyter_server_base_directory'] else os.getcwd()
print('Running Jupyter Notebook Server on {} [{}] at {}'.format(socket.gethostname(),
socket.gethostbyname(socket.gethostname()), cwd))
process = subprocess.Popen([sys.executable, '-m', 'jupyter', 'notebook', '--no-browser', '--allow-root'],
env=env, stdout=fd, stderr=fd, cwd=cwd)
# print stdout/stderr
prev_line_count = 0
while True:
process_running = True
while process_running:
process_running = False
try:
process.wait(timeout=2.0 if prev_line_count == 0 else 15.0)
except subprocess.TimeoutExpired:
with open(local_filename, "rt") as f:
# read new lines
new_lines = f.readlines()
if not new_lines:
continue
output = ''.join(new_lines)
print(output)
# update task comment with jupyter notebook server links
if prev_line_count == 0:
task.comment += '\n' + ''.join(line for line in new_lines if 'http://' in line or 'https://' in line)
prev_line_count += len(new_lines)
process_running = True
os.lseek(fd, 0, 0)
os.ftruncate(fd, 0)
continue
break
with open(local_filename, "rt") as f:
# read new lines
new_lines = f.readlines()
if not new_lines:
continue
output = ''.join(new_lines)
print(output)
# update task comment with jupyter notebook server links
if prev_line_count == 0:
task.comment += '\n' + ''.join(line for line in new_lines if 'http://' in line or 'https://' in line)
prev_line_count += len(new_lines)
os.lseek(fd, 0, 0)
os.ftruncate(fd, 0)
# cleanup
os.close(fd)