Refaktoriseringar och test av alternativa lösningar.

This commit is contained in:
2024-07-16 23:16:39 +02:00
parent 99e3737718
commit 3fef179723
4 changed files with 115 additions and 54 deletions
+26 -7
View File
@@ -4,20 +4,39 @@ import threading
from backend import run_flask
import socket
def start_frontend():
try:
# Start frontend (web server) as a separate process
subprocess.Popen(["python", "-m", "http.server", "8000"])
except Exception as e:
print(f"Failed to start frontend: {e}")
host = 'localhost' # or the address of your server
port = 8000 # change to your server's port
# Use the socket module in Python to check whether a port is in use,
# which would indicate that a server is already running on that port.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host, port))
print("No server is running on this host and port.")
# Start frontend (web server) as a separate process
subprocess.Popen(["python", "-m", "http.server", str(port)])
except socket.error as e:
if e.errno == 48:
print("Another server is already running on this host and port. Assumes it is a web server, will continue.")
else:
raise # Unexpected error, re-raise it so we can see the traceback
except Exception as e:
print(f"Failed to start frontend: {e}")
def start_backend():
try:
# Start backend as a separate thread
threading.Thread(target=run_flask).start()
# threading.Thread(target=run_flask).start() # Flask's built-in server doesn't support running in a separate thread.
run_flask()
except Exception as e:
print(f"Failed to start backend: {e}")
if __name__ == '__main__':
start_backend()
start_frontend()
start_backend()