Unify the launch of child processes (#1250)

This commit is contained in:
Sergei Ozeranskii 2023-06-27 05:50:23 +08:00 committed by GitHub
parent 73895b82b7
commit 1dfe76c4a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 14 deletions

View File

@ -14,6 +14,7 @@ from rich.progress import Progress
from reflex import constants from reflex import constants
from reflex.config import get_config from reflex.config import get_config
from reflex.utils import path_ops, prerequisites from reflex.utils import path_ops, prerequisites
from reflex.utils.processes import new_process
def update_json_file(file_path: str, update_dict: dict[str, Union[int, str]]): def update_json_file(file_path: str, update_dict: dict[str, Union[int, str]]):
@ -115,13 +116,9 @@ def export_app(
task = progress.add_task("Building app... ", total=500) task = progress.add_task("Building app... ", total=500)
# Start the subprocess with the progress bar. # Start the subprocess with the progress bar.
with progress, subprocess.Popen( with progress, new_process(
[prerequisites.get_package_manager(), "run", "export"], [prerequisites.get_package_manager(), "run", "export"],
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
env=os.environ,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE, # Redirect stdout to a pipe
universal_newlines=True, # Set universal_newlines to True for text mode
) as export_process: ) as export_process:
assert export_process.stdout is not None, "No stdout for export process." assert export_process.stdout is not None, "No stdout for export process."
for line in export_process.stdout: for line in export_process.stdout:
@ -216,7 +213,7 @@ def setup_frontend(
# Disable the Next telemetry. # Disable the Next telemetry.
if disable_telemetry: if disable_telemetry:
subprocess.Popen( new_process(
[ [
prerequisites.get_package_manager(), prerequisites.get_package_manager(),
"run", "run",
@ -225,9 +222,7 @@ def setup_frontend(
"disable", "disable",
], ],
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
env=os.environ,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
) )

View File

@ -13,6 +13,7 @@ from rich import print
from reflex import constants from reflex import constants
from reflex.config import get_config from reflex.config import get_config
from reflex.utils import console, prerequisites, processes from reflex.utils import console, prerequisites, processes
from reflex.utils.processes import new_process
from reflex.utils.watch import AssetFolderWatch from reflex.utils.watch import AssetFolderWatch
@ -36,13 +37,9 @@ def run_process_and_launch_url(
run_command: The command to run. run_command: The command to run.
loglevel: The log level to use. loglevel: The log level to use.
""" """
process = subprocess.Popen( process = new_process(
run_command, run_command,
cwd=constants.WEB_DIR, cwd=constants.WEB_DIR,
env=os.environ,
stderr=subprocess.STDOUT,
stdout=None if platform.system() == "Windows" else subprocess.PIPE,
universal_newlines=None if platform.system() == "Windows" else True,
) )
current_time = datetime.now() current_time = datetime.now()
@ -134,7 +131,7 @@ def run_backend(
loglevel, loglevel,
"--reload", "--reload",
] ]
process = subprocess.Popen(cmd) process = new_process(cmd)
try: try:
process.wait() process.wait()

View File

@ -5,6 +5,7 @@ from __future__ import annotations
import contextlib import contextlib
import os import os
import signal import signal
import subprocess
import sys import sys
from typing import Optional from typing import Optional
from urllib.parse import urlparse from urllib.parse import urlparse
@ -120,3 +121,27 @@ def change_or_terminate_port(port, _type) -> str:
else: else:
console.print("Exiting...") console.print("Exiting...")
sys.exit() sys.exit()
def new_process(args, **kwargs):
"""Wrapper over subprocess.Popen to unify the launch of child processes.
Args:
args: A string, or a sequence of program arguments.
**kwargs: Kwargs to override default wrap values to pass to subprocess.Popen as arguments.
Returns:
Execute a child program in a new process.
"""
kwargs = {
"env": os.environ,
"stderr": subprocess.STDOUT,
"stdout": subprocess.PIPE, # Redirect stdout to a pipe
"universal_newlines": True, # Set universal_newlines to True for text mode
"encoding": "UTF-8",
**kwargs,
}
return subprocess.Popen(
args,
**kwargs,
)