diff --git a/py/h2o_wave/h2o_wave/cli.py b/py/h2o_wave/h2o_wave/cli.py index 9db1a33214..4a30294bb1 100644 --- a/py/h2o_wave/h2o_wave/cli.py +++ b/py/h2o_wave/h2o_wave/cli.py @@ -17,7 +17,6 @@ import platform import shutil import socket -import subprocess import sys import tarfile import time @@ -25,6 +24,7 @@ from pathlib import Path from urllib import request from urllib.parse import urlparse +from subprocess import Popen import click import httpx @@ -103,8 +103,8 @@ def run(app: str, no_reload: bool, no_autostart: bool): """ app_address = urlparse(os.environ.get('H2O_WAVE_APP_ADDRESS', f'http://{_localhost}:{_scan_free_port()}')) - host = app_address.hostname - port = app_address.port + host = app_address.hostname or _localhost + port = app_address.port or 8000 addr = f'http://{host}:{port}' os.environ['H2O_WAVE_INTERNAL_ADDRESS'] = addr # TODO deprecated @@ -126,6 +126,7 @@ def run(app: str, no_reload: bool, no_autostart: bool): server_not_running = _scan_free_port(server_port) == server_port waved_process = None + wave_app_process = None if no_autostart: autostart = False else: @@ -137,7 +138,7 @@ def run(app: str, no_reload: bool, no_autostart: bool): try: if autostart and is_waved_present and server_not_running: - waved_process = subprocess.Popen([waved], cwd=sys.exec_prefix, env=os.environ.copy(), shell=True) + waved_process = Popen([waved], cwd=sys.exec_prefix, env=os.environ.copy(), shell=True) time.sleep(1) server_not_running = _scan_free_port(server_port) == server_port retries = 3 @@ -156,11 +157,19 @@ def run(app: str, no_reload: bool, no_autostart: bool): reload_exclude = os.environ.get('H2O_WAVE_RELOAD_EXCLUDE', None) if reload_exclude: reload_exclude = reload_exclude.split(os.pathsep) - uvicorn.run(f'{app}:main', host=host, port=port, reload=not no_reload, reload_excludes=reload_exclude) - except Exception as e: + args = [sys.executable, '-m', 'uvicorn', f'{app}:main', '--host', host, '--port', str(port)] + if not no_reload: + args.append('--reload') + if reload_exclude: + for exclude in reload_exclude: + args.extend(['--reload-exclude', exclude]) + wave_app_process = Popen(args, env=os.environ.copy()) + wave_app_process.communicate() + finally: if waved_process: waved_process.kill() - raise e + if wave_app_process: + wave_app_process.kill() @main.command()