Skip to content

Commit

Permalink
Replace asyncio.create_subprocess_exec() with python subprocess for t…
Browse files Browse the repository at this point in the history
…our (#429)

* Replace asyncio.create_subprocess_exec() with python subprocess for tour

Windows does not support SelectorEventLoop. To support tour in all the platforms, replaced asyncio.create_subprocess_exec() with python subprocess. Fixes: #397

* Introduce review suggestions

* Introduce local variable to hold the reference to env

* Introduce local variable to hold the reference to env
  • Loading branch information
VijithaEkanayake authored Dec 22, 2020
1 parent d4bac94 commit 08761e1
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions py/examples/tour.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import collections
import os
import os.path
import subprocess
import sys
from typing import List, Optional, Dict

Expand Down Expand Up @@ -29,24 +29,24 @@ def __init__(self, filename: str, title: str, description: str, source: str):
self.code = highlight(source, py_lexer, html_formatter)
self.previous_example: Optional[Example] = None
self.next_example: Optional[Example] = None
self.process: Optional[asyncio.subprocess.Process] = None
self.process: Optional[subprocess.Popen] = None
self.is_app = source.find('@app(') > 0

async def start(self):
# The environment passed into Popen must include SYSTEMROOT, otherwise Popen will fail when called
# inside python during initialization if %PATH% is configured, but without %SYSTEMROOT%.
env = {'SYSTEMROOT': os.environ['SYSTEMROOT']} if sys.platform.lower().startswith('win') else {}
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=dict(H2O_WAVE_EXTERNAL_ADDRESS=f'http://{_app_host}:{_app_port}', **env))
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)], env=env)

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


active_example: Optional[Example] = None
Expand Down

0 comments on commit 08761e1

Please sign in to comment.