Skip to content

Commit

Permalink
Merge pull request #139 from Renumics/feature/pass-socket-as-fd
Browse files Browse the repository at this point in the history
Feature/pass socket as fd
  • Loading branch information
druzsan authored Jul 11, 2023
2 parents 434b645 + 8d476d5 commit ede3416
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions renumics/spotlight/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Local proxy object for the spotlight server process
"""

import platform
import threading
from queue import Queue, Empty
import socket
Expand Down Expand Up @@ -118,12 +119,10 @@ def start(self, config: AppConfig) -> None:
self._vite.start()
env["VITE_URL"] = self._vite.url

# automatic port selection
if self._requested_port == 0:
with socket.socket() as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self._host, self._port))
self._port = sock.getsockname()[1]
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self._host, self._port))
self._port = sock.getsockname()[1]

command = [
sys.executable,
Expand All @@ -132,8 +131,6 @@ def start(self, config: AppConfig) -> None:
"renumics.spotlight.app:SpotlightApp",
"--host",
self._host,
"--port",
str(self._port),
"--log-level",
"critical",
"--http",
Expand All @@ -144,14 +141,24 @@ def start(self, config: AppConfig) -> None:
str(2),
"--factory",
]
if platform.system() == "Windows":
command += ["--port", str(self._port)]
sock.close()
else:
command += ["--fd", str(sock.fileno())]

if settings.dev:
command.extend(["--reload"])

# start uvicorn
# pylint: disable=consider-using-with
self.process = subprocess.Popen(command, env=env)

self.process = subprocess.Popen(
command,
env=env,
pass_fds=None if platform.system() == "Windows" else (sock.fileno(),),
)
if platform.system() != "Windows":
sock.close()
self._startup_complete_event.wait(timeout=120)

def stop(self) -> None:
Expand All @@ -169,6 +176,7 @@ def stop(self) -> None:
self.process.wait(3)
except subprocess.TimeoutExpired:
self.process.kill()
self.process.wait(timeout=5)
self.process = None

self._connection_thread.join(0.1)
Expand Down

0 comments on commit ede3416

Please sign in to comment.