Quellcode durchsuchen

port auto ranges (#1519) (#1679)

Dinh Long Nguyen vor 9 Monaten
Ursprung
Commit
6611ed5d3c
5 geänderte Dateien mit 29 neuen und 6 gelöschten Zeilen
  1. 1 0
      taipy/gui/_default_config.py
  2. 2 0
      taipy/gui/config.py
  3. 1 0
      taipy/gui/gui.py
  4. 24 6
      taipy/gui/server.py
  5. 1 0
      tests/gui/helpers.py

+ 1 - 0
taipy/gui/_default_config.py

@@ -59,6 +59,7 @@ default_config: Config = {
     "notebook_proxy": True,
     "notification_duration": 3000,
     "port": 5000,
+    "port_auto_ranges": [(49152, 65535)],
     "propagate": True,
     "run_browser": True,
     "run_in_thread": False,

+ 2 - 0
taipy/gui/config.py

@@ -47,6 +47,7 @@ ConfigParameter = t.Literal[
     "notebook_proxy",
     "notification_duration",
     "port",
+    "port_auto_ranges",
     "propagate",
     "run_browser",
     "run_in_thread",
@@ -119,6 +120,7 @@ Config = t.TypedDict(
         "notebook_proxy": bool,
         "notification_duration": int,
         "port": t.Union[t.Literal["auto"], int],
+        "port_auto_ranges": t.List[t.Union[int, t.Tuple[int, int]]],
         "propagate": bool,
         "run_browser": bool,
         "run_in_thread": bool,

+ 1 - 0
taipy/gui/gui.py

@@ -2751,6 +2751,7 @@ class Gui:
             run_in_thread=app_config["run_in_thread"],
             allow_unsafe_werkzeug=app_config["allow_unsafe_werkzeug"],
             notebook_proxy=app_config["notebook_proxy"],
+            port_auto_ranges=app_config["port_auto_ranges"]
         )
 
     def reload(self):  # pragma: no cover

+ 24 - 6
taipy/gui/server.py

@@ -21,7 +21,7 @@ import time
 import typing as t
 import webbrowser
 from importlib import util
-from random import randint
+from random import choices, randint
 
 from flask import Blueprint, Flask, json, jsonify, render_template, request, send_from_directory
 from flask_cors import CORS
@@ -258,7 +258,7 @@ class _Server:
         if self._get_async_mode() == "gevent" and util.find_spec("gevent"):
             from gevent import get_hub, monkey
 
-            get_hub().NOT_ERROR += (KeyboardInterrupt, )
+            get_hub().NOT_ERROR += (KeyboardInterrupt,)
             if not monkey.is_module_patched("time"):
                 monkey.patch_time()
         if self._get_async_mode() == "eventlet" and util.find_spec("eventlet"):
@@ -267,17 +267,35 @@ class _Server:
             if not patcher.is_monkey_patched("time"):
                 monkey_patch(time=True)
 
-    def _get_random_port(self):  # pragma: no cover
+    def _get_random_port(
+        self, port_auto_ranges: t.Optional[t.List[t.Union[int, t.Tuple[int, int]]]] = None
+    ):  # pragma: no cover
+        port_auto_ranges = port_auto_ranges or [(49152, 65535)]
+        random_weights = [1 if isinstance(r, int) else abs(r[1] - r[0]) + 1 for r in port_auto_ranges]
         while True:
-            port = randint(49152, 65535)
+            random_choices = [
+                r if isinstance(r, int) else randint(min(r[0], r[1]), max(r[0], r[1])) for r in port_auto_ranges
+            ]
+            port = choices(random_choices, weights=random_weights)[0]
             if port not in _RuntimeManager().get_used_port() and not _is_port_open(self._host, port):
                 return port
 
-    def run(self, host, port, debug, use_reloader, flask_log, run_in_thread, allow_unsafe_werkzeug, notebook_proxy):
+    def run(
+        self,
+        host,
+        port,
+        debug,
+        use_reloader,
+        flask_log,
+        run_in_thread,
+        allow_unsafe_werkzeug,
+        notebook_proxy,
+        port_auto_ranges,
+    ):
         host_value = host if host != "0.0.0.0" else "localhost"
         self._host = host
         if port == "auto":
-            port = self._get_random_port()
+            port = self._get_random_port(port_auto_ranges)
         self._port = port
         if _is_in_notebook() and notebook_proxy:  # pragma: no cover
             from .utils.proxy import NotebookProxy

+ 1 - 0
tests/gui/helpers.py

@@ -153,6 +153,7 @@ class Helpers:
                 run_in_thread=True,
                 allow_unsafe_werkzeug=False,
                 notebook_proxy=False,
+                port_auto_ranges=gui._get_config("port_auto_ranges", None),
             )
         while not Helpers.port_check():
             time.sleep(0.1)