Spaces:
Sleeping
Sleeping
| import subprocess | |
| import time | |
| import threading | |
| import random | |
| from http.server import BaseHTTPRequestHandler, HTTPServer | |
| def run_version_loop(python_bin, version_name): | |
| # Determine a completely random number of total loops up to 30,000 | |
| max_loops = random.randint(1000, 30000) | |
| print(f"[{version_name}] Started. Targeted loop count set randomly to: {max_loops}", flush=True) | |
| # FIX: Added --break-system-packages to bypass PEP 668 safety limits in Docker | |
| subprocess.run([python_bin, "-m", "pip", "install", "-q", "--break-system-packages", "npmai-agents"]) | |
| subprocess.run([python_bin, "-m", "pip", "uninstall", "-q", "-y", "--break-system-packages", "npmai-agents", "npmai"]) | |
| iteration = 1 | |
| while iteration <= max_loops: | |
| print(f"[{version_name}] Iteration {iteration} of {max_loops}", flush=True) | |
| # Fast installation pull with system package override | |
| subprocess.run([python_bin, "-m", "pip", "install", "-q", "--break-system-packages", "npmai-agents"]) | |
| # Clean package removal with system package override | |
| subprocess.run([python_bin, "-m", "pip", "uninstall", "-q", "-y", "--break-system-packages", "npmai-agents", "npmai"]) | |
| iteration += 1 | |
| time.sleep(0.1) # 100ms delay to keep CPU stable | |
| print(f"[{version_name}] SUCCESS: Reached target cap of {max_loops} loops. Thread stopped.", flush=True) | |
| class MultiVersionHealthServer(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| self.send_response(200) | |
| self.send_header("Content-type", "text/plain") | |
| self.end_headers() | |
| self.wfile.write(b"Multi-version execution container running Python 3.11, 3.12, and 3.13.") | |
| def log_message(self, format, *args): | |
| return # Silences internal server network logs | |
| if __name__ == "__main__": | |
| environments = [ | |
| {"bin": "python3.11", "label": "Python 3.11"}, | |
| {"bin": "python3.12", "label": "Python 3.12"}, | |
| {"bin": "python3.13", "label": "Python 3.13"} | |
| ] | |
| for env in environments: | |
| thread = threading.Thread( | |
| target=run_version_loop, | |
| args=(env["bin"], env["label"]), | |
| daemon=True | |
| ) | |
| thread.start() | |
| server_address = ('0.0.0.0', 7860) | |
| httpd = HTTPServer(server_address, MultiVersionHealthServer) | |
| print("Multi-version server online and listening on port 7860.", flush=True) | |
| httpd.serve_forever() | |