Skip to content

Commit

Permalink
Replace asyncio.create_subprocess_exec() with python subprocess for tour
Browse files Browse the repository at this point in the history
Windows does not support SelectorEventLoop. To support tour in all the platforms, replaced asyncio.create_subprocess_exec() with python subprocess. Fixes: #397
  • Loading branch information
VijithaEkanayake committed Dec 21, 2020
1 parent 94cd7cc commit bfc2495
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions py/examples/tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path
import sys
from typing import List, Optional, Dict
import subprocess

from pygments import highlight
from pygments.formatters.html import HtmlFormatter
Expand Down Expand Up @@ -34,19 +35,40 @@ def __init__(self, filename: str, title: str, description: str, source: str):

async def start(self):
if self.is_app:
self.process = await asyncio.create_subprocess_exec(
sys.executable, '-m', 'uvicorn', '--port', _app_port, f'examples.{self.name}:main', env=dict(
H2O_WAVE_EXTERNAL_ADDRESS=f'http://{_app_host}:{_app_port}'
),
)
self.process = subprocess.Popen(
[sys.executable, '-m', 'uvicorn', '--port', _app_port, f'examples.{self.name}:main'],
env=self.adjust_env_for_platform(dict(H2O_WAVE_EXTERNAL_ADDRESS=f'http://{_app_host}:{_app_port}')))
else:
self.process = await asyncio.create_subprocess_exec(
sys.executable, os.path.join(example_dir, self.filename)
)
self.process = subprocess.Popen([sys.executable, os.path.join(example_dir, self.filename)])

async def stop(self):
if self.process and self.process.returncode is None:
self.process.terminate()
self.process.wait()

def adjust_env_for_platform(self, env):
""" Add required platform-specific adjustments to env.
"""
if sys.platform.startswith('win'):
self._add_systemroot_to_env_win32(env)

def _add_systemroot_to_env_win32(self, env):
""" Sets ``%SYSTEMROOT%`` environment variable, if not present in :py:attr:`target_environ` .
Args:
env (dict): desired environment variables
"""
# 'SYSTEMROOT' unnecessary unless 'PATH' is set.
if env is None:
return
# leave SYSTEMROOT alone if set by user
if 'SYSTEMROOT' in env:
return
# not enough info to set SYSTEMROOT
if 'SYSTEMROOT' not in os.environ:
return

env['SYSTEMROOT'] = os.environ['SYSTEMROOT']


active_example: Optional[Example] = None
Expand Down

0 comments on commit bfc2495

Please sign in to comment.