Spaces:
Runtime error
Runtime error
| 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() | |