File size: 1,078 Bytes
064d5a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import subprocess
import sys
import time
from threading import Thread
import os
import signal

def run_fastapi():
    print("Starting FastAPI server...")
    subprocess.run([sys.executable, "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"])

def run_streamlit():
    print("Starting Streamlit server...")
    subprocess.run([sys.executable, "-m", "streamlit", "run", "app.py"])

def main():
    # Start FastAPI in a separate thread
    fastapi_thread = Thread(target=run_fastapi)
    fastapi_thread.daemon = True
    fastapi_thread.start()

    # Give FastAPI a moment to start
    time.sleep(2)

    # Start Streamlit
    streamlit_thread = Thread(target=run_streamlit)
    streamlit_thread.daemon = True
    streamlit_thread.start()

    try:
        # Keep the main thread alive
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("\nShutting down servers...")
        # Send SIGTERM to the current process group
        os.killpg(os.getpgid(0), signal.SIGTERM)
        sys.exit(0)

if __name__ == "__main__":
    main()