File size: 1,268 Bytes
c2fb337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
import subprocess
import time
import requests
import os


def start_server(port):
    env = os.environ.copy()
    env["PORT"] = str(port)
    print("Starting server on port", port)
    proc = subprocess.Popen(
        ["C:\\Users\\CINDY\\AppData\\Local\\Programs\\Python\\Python310\\python.exe", "app.py"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        env=env,
    )
    return proc


def wait_until_up(url, timeout=10.0):
    start = time.time()
    while True:
        try:
            r = requests.post(url, json={"features": [200, 1, 0, 500]}, timeout=1.0)
            print("server responded", r.status_code, r.json())
            return True
        except Exception as e:
            if time.time() - start > timeout:
                print("timeout waiting for server", e)
                return False
            time.sleep(0.2)


def main():
    port = 5002
    url = f"http://127.0.0.1:{port}/predict"
    proc = start_server(port)
    try:
        ok = wait_until_up(url)
        print("wait result:", ok)
    finally:
        proc.terminate()
        try:
            proc.wait(timeout=5)
        except Exception:
            proc.kill()


if __name__ == '__main__':
    main()