| 164 | return tokens, time.perf_counter() - t0 |
| 165 | |
| 166 | def stop(self) -> None: |
| 167 | if self.proc is None: |
| 168 | return |
| 169 | try: |
| 170 | if self.proc.stdin: |
| 171 | self.proc.stdin.write(b"quit\n") |
| 172 | self.proc.stdin.flush() |
| 173 | self.proc.stdin.close() |
| 174 | except Exception: |
| 175 | pass |
| 176 | try: |
| 177 | self.proc.wait(timeout=10) |
| 178 | except subprocess.TimeoutExpired: |
| 179 | self.proc.terminate() |
| 180 | try: |
| 181 | self.proc.wait(timeout=5) |
| 182 | except subprocess.TimeoutExpired: |
| 183 | self.proc.kill() |
| 184 | self.proc.wait() |
| 185 | if self.r_fd is not None: |
| 186 | try: |
| 187 | os.close(self.r_fd) |
| 188 | except OSError: |
| 189 | pass |
| 190 | self.proc = None |
| 191 | |
| 192 | |
| 193 | class GpuMonitor: |