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

Improve Multithreading Implementation in MAIN.PY #60 #67

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 28 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
from __future__ import annotations

import logging
import sys
from concurrent.futures import as_completed
from concurrent.futures import ThreadPoolExecutor

from API import run_fastapi_app
from FaceRec.app.main import run_flask

# Multithreading used to start both FastAPI and Flask apps at the same time.

def start_apps():
"""Starts FastAPI and Flask applications concurrently."""
try:
# We are using ThreadPoolExecutor for concurrent execution
with ThreadPoolExecutor(max_workers=2) as executor:
futures = {
executor.submit(run_flask): 'Flask', # to start flask app
# to start fastapi app
executor.submit(run_fastapi_app): 'FastAPI',
}
# for monitoring the apps
for future in as_completed(futures):
app_name = futures[future]
try:
future.result() # Checks for Errors
except Exception as e:
logging.error(
f"{app_name} application failed to start: {e}")
sys.exit(1) # Exit if any application fails
except Exception as e:
logging.error(f"An error occurred while starting applications: {e}")


if __name__ == '__main__':
with ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(run_flask)
executor.submit(run_fastapi_app)
start_apps() # for running apps
Binary file modified requirements.txt
Binary file not shown.
Loading