Skip to content

Commit

Permalink
fix: Run uvicorn in a separate process rather than a main one. #2074
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Jul 21, 2023
1 parent 96fdfd7 commit 8ea11fb
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions py/h2o_wave/h2o_wave/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import platform
import shutil
import socket
import subprocess
import sys
import tarfile
import time
from contextlib import closing
from pathlib import Path
from urllib import request
from urllib.parse import urlparse
from subprocess import Popen

import click
import httpx
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 8ea11fb

Please sign in to comment.