Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Minor fixes (#991)
Browse files Browse the repository at this point in the history
* Minor fixes

* verify port before starting
  • Loading branch information
dinhlongviolin1 authored Oct 23, 2023
1 parent 75274cd commit 88e99db
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,9 +1629,11 @@ def _broadcast(self, name: str, value: t.Any):
self.__send_ws_broadcast(name, value)

def _broadcast_all_clients(self, name: str, value: t.Any):
self._set_broadcast()
self._update_var(name, value)
self._set_broadcast(False)
try:
self._set_broadcast()
self._update_var(name, value)
finally:
self._set_broadcast(False)

def _set_broadcast(self, broadcast: bool = True):
with contextlib.suppress(RuntimeError):
Expand Down
15 changes: 4 additions & 11 deletions src/taipy/gui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import os
import pathlib
import re
import socket
import sys
import time
import typing as t
Expand All @@ -36,7 +35,7 @@

from ._renderers.json import _TaipyJsonProvider
from .config import ServerConfig
from .utils import _is_in_notebook, _RuntimeManager
from .utils import _is_in_notebook, _is_port_open, _RuntimeManager
from .utils.proxy import NotebookProxy

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -237,12 +236,6 @@ def _run_notebook(self):
def _get_async_mode(self) -> str:
return self._ws.async_mode

def _is_port_open(self, host, port) -> bool:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((host, port))
sock.close()
return result == 0

def _apply_patch(self):
if self._get_async_mode() == "gevent" and util.find_spec("gevent"):
from gevent import monkey
Expand All @@ -258,7 +251,7 @@ def _apply_patch(self):
def _get_random_port(self): # pragma: no cover
while True:
port = randint(49152, 65535)
if port not in _RuntimeManager().get_used_port() and not self._is_port_open(self._host, port):
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):
Expand All @@ -273,7 +266,7 @@ def run(self, host, port, debug, use_reloader, flask_log, run_in_thread, allow_u
if _is_in_notebook() or run_in_thread:
runtime_manager = _RuntimeManager()
runtime_manager.add_gui(self._gui, port)
if debug and not is_running_from_reloader() and self._is_port_open(host_value, port):
if debug and not is_running_from_reloader() and _is_port_open(host_value, port):
raise ConnectionError(
f"Port {port} is already opened on {host_value}. You have another server application running on the same port."
)
Expand Down Expand Up @@ -316,7 +309,7 @@ def stop_thread(self):
self._thread.kill()
else:
self._thread.kill()
while self._is_port_open(self._host, self._port):
while _is_port_open(self._host, self._port):
time.sleep(0.1)

def stop_proxy(self):
Expand Down
1 change: 1 addition & 0 deletions src/taipy/gui/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .getdatecolstrname import _RE_PD_TYPE, _get_date_col_str_name
from .html import _get_css_var_value
from .is_debugging import is_debugging
from .is_port_open import _is_port_open
from .isnotebook import _is_in_notebook
from .types import (
_TaipyBase,
Expand Down
19 changes: 19 additions & 0 deletions src/taipy/gui/utils/is_port_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Avaiga Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import socket


def _is_port_open(host, port) -> bool:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((host, port))
sock.close()
return result == 0
13 changes: 10 additions & 3 deletions src/taipy/gui/utils/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET, Site

from .is_port_open import _is_port_open
from .singleton import _Singleton

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -93,10 +94,16 @@ def __init__(self, gui: "Gui", listening_port: int) -> None:
def run(self):
if self._is_running:
return
self._is_running = True
site = Site(_TaipyReverseProxyResource(self._gui._get_config("host", "127.0.0.1"), b"", self._gui))
reactor.listenTCP(self._listening_port, site)
host = self._gui._get_config("host", "127.0.0.1")
port = self._listening_port
if _is_port_open(host, port):
raise ConnectionError(
f"Port {port} is already opened on {host}. You have another server application running on the same port."
)
site = Site(_TaipyReverseProxyResource(host, b"", self._gui))
reactor.listenTCP(port, site)
Thread(target=reactor.run, args=(False,)).start()
self._is_running = True

def stop(self):
if not self._is_running:
Expand Down

0 comments on commit 88e99db

Please sign in to comment.