Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alek/port #388

Merged
merged 4 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pynecone/pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,14 @@ def run(
assert frontend_cmd and backend_cmd, "Invalid env"

# Run the frontend and backend.
if frontend:
frontend_cmd(app.app, Path.cwd(), port)
if backend:
backend_cmd(app.__name__, loglevel=loglevel)
try:
if frontend:
frontend_cmd(app.app, Path.cwd(), port)
if backend:
backend_cmd(app.__name__, loglevel=loglevel)
finally:
utils.kill_process_on_port(os.environ["PORT"])
utils.kill_process_on_port(utils.get_api_port())


@cli.command()
Expand Down
18 changes: 17 additions & 1 deletion pynecone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Union,
)
from urllib.parse import urlparse

import psutil
import plotly.graph_objects as go
import typer
import uvicorn
Expand Down Expand Up @@ -582,6 +582,22 @@ def get_api_port() -> int:
return port


def kill_process_on_port(port):
"""Kill the process on the given port.

Args:
port: The port.
"""
for proc in psutil.process_iter(["pid", "name", "cmdline"]):
try:
for conns in proc.connections(kind="inet"):
if conns.laddr.port == port:
proc.kill()
return
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass


def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR):
"""Run the backend.

Expand Down