Start the server in a background thread
(self)
| 29 | self.thread = None |
| 30 | |
| 31 | def start(self): |
| 32 | """Start the server in a background thread""" |
| 33 | # Kill any existing process on port 8000 |
| 34 | print("Checking for existing server on port 8000...") |
| 35 | result = subprocess.run(['lsof', '-ti:8000'], capture_output=True, text=True) |
| 36 | if result.stdout.strip(): |
| 37 | for pid in result.stdout.strip().split('\n'): |
| 38 | if pid: |
| 39 | try: |
| 40 | os.kill(int(pid), signal.SIGTERM) |
| 41 | print(f"Killed existing process {pid}") |
| 42 | except: |
| 43 | pass |
| 44 | time.sleep(1) |
| 45 | |
| 46 | self.running = True |
| 47 | self.thread = threading.Thread(target=self._run_server, daemon=True) |
| 48 | self.thread.start() |
| 49 | |
| 50 | # Wait for server to start |
| 51 | print("Starting server in background thread...") |
| 52 | timeout = time.time() + 20 |
| 53 | started = False |
| 54 | |
| 55 | while time.time() < timeout and self.running: |
| 56 | try: |
| 57 | line = self.output_queue.get(timeout=0.1) |
| 58 | print(f"[SERVER] {line}") |
| 59 | if "Server started at" in line: |
| 60 | started = True |
| 61 | break |
| 62 | except queue.Empty: |
| 63 | continue |
| 64 | |
| 65 | if started: |
| 66 | print("\n✅ Server is running on http://localhost:8000\n") |
| 67 | return True |
| 68 | else: |
| 69 | print("\n❌ Server failed to start\n") |
| 70 | self.stop() |
| 71 | return False |
| 72 | |
| 73 | def _run_server(self): |
| 74 | """Run the server process""" |
no test coverage detected